问题描述
packagedbutil;importjava.io.*;importjava.net.*;publicclassSocketServer{ServerSocketss=null;Sockets=null;DataInputStreaminStream=null;DataOutputStreamoutStream=null;publicSocketServer(){try{init();}catch(Exceptione){System.out.println(e.toString());}}voidinit()throwsException{ss=newServerSocket(9999);}voidwaitForClient(){System.out.println("serverbegin");try{s=ss.accept();inStream=newDataInputStream(s.getInputStream());outStream=newDataOutputStream(s.getOutputStream());outStream.writeUTF("1");s.setSoTimeout(3000);waitData();}catch(Exceptione){System.out.println(e.toString());}}voidwaitData(){while(true){try{Stringstr=inStream.readUTF();System.out.println("Serveraccept:"+str);intnu=Integer.parseInt(str)+1;if(nu>20){System.out.println("Sendend!");s.close();inStream.close();outStream.close();break;}else{str=Integer.toString(nu);outStream.writeUTF(str);}}catch(Exceptione){System.out.println("error:"+e.toString());break;}}}publicstaticvoidmain(String[]args){SocketServersocketserver=newSocketServer();socketserver.waitForClient();}}packagedbutil;importjava.net.*;importjava.io.*;publicclassSocketClient{Sockets=null;DataInputStreaminStream=null;DataOutputStreamoutStream=null;publicSocketClient(){try{init();waitData();}catch(Exceptione){System.out.println(e.toString());}}voidinit()throwsException{s=newSocket("localhost",9999);//把这里的IP改成你运行SocketServer.class的IPinStream=newDataInputStream(s.getInputStream());outStream=newDataOutputStream(s.getOutputStream());s.setSoTimeout(3000);}voidwaitData(){while(true){try{Stringstr=inStream.readUTF();System.out.println("Clientaccept:"+str);str=Integer.toString(Integer.parseInt(str)+1);outStream.writeUTF(str);}catch(EOFExceptione){System.out.println("Clientterminal.");break;}catch(Exceptione){System.out.println(e.toString());break;}}}publicstaticvoidmain(String[]args){SocketClientsocketClient1=newSocketClient();}}为何我执行client时提示eofexception,然后server也断开了呢。。
解决方案
解决方案二:
代码里写了注释packagecom;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.net.ServerSocket;importjava.net.Socket;publicclassSocketServer{ServerSocketss=null;Sockets=null;DataInputStreaminStream=null;DataOutputStreamoutStream=null;publicSocketServer(){try{init();}catch(Exceptione){System.out.println(e.toString());}}voidinit()throwsException{ss=newServerSocket(9999);}voidwaitForClient(){System.out.println("serverbegin");try{s=ss.accept();inStream=newDataInputStream(s.getInputStream());outStream=newDataOutputStream(s.getOutputStream());outStream.writeUTF("1");s.setSoTimeout(30000);waitData();}catch(Exceptione){System.out.println(e.toString());}}voidwaitData(){while(true){try{Stringstr=inStream.readUTF();System.out.println("Serveraccept:"+str);intnu=Integer.parseInt(str)+1;//侦听客户端的回应如果是exit就退出if(str.equals("exit")){s.close();inStream.close();outStream.close();}//下面的代码你传了strstr在加啊客户端加了传给服务端,服务端加了又才传给客户端==20就close();了//if(nu>20){//System.out.println("Sendend!");//s.close();//inStream.close();//outStream.close();//break;//}/*else{str=Integer.toString(nu);outStream.writeUTF(str);}*/}catch(Exceptione){System.out.println("error:"+e.toString());break;}}}publicstaticvoidmain(String[]args){SocketServersocketserver=newSocketServer();socketserver.waitForClient();}}
packagecom;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.EOFException;importjava.net.Socket;importjava.util.Scanner;publicclassSocketClient{Sockets=null;DataInputStreaminStream=null;DataOutputStreamoutStream=null;publicSocketClient(){try{init();waitData();}catch(Exceptione){System.out.println(e.toString());}}voidinit()throwsException{s=newSocket("127.0.0.1",9999);//把这里的IP改成你运行SocketServer.class的IPinStream=newDataInputStream(s.getInputStream());outStream=newDataOutputStream(s.getOutputStream());//s.setSoTimeout(3000);}voidwaitData(){while(true){try{Stringstr=inStream.readUTF();System.out.println("Clientaccept:"+str);str=Integer.toString(Integer.parseInt(str)+1);System.out.println("请输入exit退出:");//控制台输入exitScanners=newScanner(System.in);Stringst=s.nextLine();outStream.writeUTF(st);}catch(EOFExceptione){System.out.println("Clientterminal.");break;}catch(Exceptione){System.out.println(e.toString());break;}}}publicstaticvoidmain(String[]args){SocketClientsocketClient1=newSocketClient();}}
解决方案三:
catch(Exceptione){System.out.println("error:"+e.toString());break;}将break去掉
解决方案四:
多谢,好像还是有点问题啊