Today, when writing socket data to communicate with each other, encountered a bug and its egg pain. There is no problem with the server and client when transferring data in one direction, but the problem arises when all two have input and output operations Java.net.SocketException:Socket is closed:
The following code is attached:
Server:
StringBuffer result =NewStringBuffer (""); intPort = 9090; //define a ServerSocket listener on port 9090ServerSocket Server =NULL; while(true) {//note where the while is placedTry{Server=NewServerSocket (port); //server attempts to receive connection requests from other sockets, the server's accept method is blockedSocket client =server.accept (); Server.setsotimeout (20*1000); System.out.println ("Client connected!"); //Receiving MessagesReader Read =NewInputStreamReader (Client.getinputstream ()); BufferedReader Bufferreader=NewBufferedReader (read); String ReadLine= ""; //Bufferreader.readline () read one line at a time, use it before, and then use it for the next line . while((ReadLine = Bufferreader.readline ())! =NULL) {result.append (readLine); } bufferreader.close;System.out.println ("From client:" +result.tostring ()); //Boolean isconnected = socket.isconnected () &&!socket.isclosed (); //after reading, write a sentenceOutputStream OS =Client.getoutputstream ();//This line will report the socket close error DataOutputStream out=Newdataoutputstream (OS); Out.writebytes ("Hello client,i ' m server!"); System.out.println ("Sent Mesg"); Out.flush ();//Empty Cacheout.close ();//CloseClient.close (); } Catch(IOException e) {e.printstacktrace (); } finally{//here Socket.close best put in finallyTry { if(Server! =NULL) {server.close (); } }Catch(IOException e) {e.printstacktrace (); } }
Client:
StringBuffer result=NewStringBuffer ("") ; String Host= "192.168.0.88"; intPort = 9090; Socket Client=NULL; Try{System.out.println ("Connecting to" + Host + "on port" +port); Client=NewSocket (host, Port); Client.setsotimeout (20*1000); System.out.println ("Just connected to" +client.getremotesocketaddress ());
Send Message OutputStream OS=Client.getoutputstream (); DataOutputStream out=Newdataoutputstream (OS); Out.writebytes ("Hello server!"); System.out.println ("Sent Mesg"); Out.flush ();//Empty Cacheout.close ();//Close//Receiving MessagesReader Read =NewInputStreamReader (Client.getinputstream ());//Also here will be an error BufferedReader Bufferreader=NewBufferedReader (read); String ReadLine= ""; while((ReadLine = Bufferreader.readline ())! =NULL) {result.append (readLine); } System.out.println ("From Server:" +result.tostring ()); Bufferreader.close; }Catch(IOException e) {e.printstacktrace (); } finally { Try { if(Client! =NULL) {client.close (); } }Catch(IOException e) {e.printstacktrace (); } }
Looking for a long time finally found Bufferreader.close () and Out.close () close will directly lead to Sockect.close () so only enter the fire Rat Junior high School after one appears Java.net.SocketException:Socket Is closed: error, possibly because of the use of Socket.getoutputstream (), Socket.getinputstream ().
This will convert Out.close () and Bufferreader.close () to Client.shutdownoutput () and Client.shutdowninput (), and there will be no problem with the socket being closed directly.
Socket.close (), input and output streams are off, and sometimes you want to just close one of the input or output streams->socket semi-close method,
Shutdowninput (): Close the input stream only
Shutdownoutput (): Turn off the output stream only
If you put bufferreader.close () and Out.close () at the end of this question, the server will appear in read because the client is not closed OutputStream has been silly wait. Therefore, the method is not advisable.
Java Socket Communication Example and about Java.net.SocketException:Socket is closed error