简体   繁体   中英

Java SocketException Broken pipe

I am doing a client to server Log-in communication.

I met a java.net.SocketException: broke Pipe at Server end. And I have narrowed the problem to one single line at the client end . If I move a position for this line, the code works. plese see the following code.

Client End:

    Socket socket = new Socket(Const.destIp, 12101);
    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
    out.writeObject(this.message);              
    out.close();//Line that cause problem   
    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
    ServerToClientLogin msg = (ServerToClientLogin) in.readObject();
    //out.close();//move it to here, problem solved
    in.close();
    socket.close();

Server end:

   while (true) {
     socket = _serverSocket.accept();
     in = new ObjectInputStream(socket.getInputStream());
     msg = (ClientToServerLogin) in.readObject();

     ServerToClientLogin msgToSend = null;
     out = new ObjectOutputStream(socket.getOutputStream());
     msgToSend = handleLoginRequest(msg);
     if(msgToSend != null) out.writeObject(msgToSend);

     try { in.close(); } catch (IOException e) {e.printStackTrace();}
     try { out.close();} catch (IOException e) {e.printStackTrace(); }
     try { socket.close();} catch (IOException e) {e.printStackTrace();}

}

Since readObject and writeObject are blocking call, I have no idea why close it earlier would case such problem.

out.close(); : Closes this ( out ) output stream and releases any system resources associated with this stream.

See the API here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM