1

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.

1 Answer 1

1

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

See the API here.

Sign up to request clarification or add additional context in comments.

4 Comments

out is of type ObjectOutputStream, calling out.close() would close its socket?
Closes this output stream and releases any system resources associated with this stream, that's in the API
@PbxMan add what you said to the answer, it'll make it clearer.
@PbxMan For clarity, yes it closes the socket, also its input stream. Closing any one of those three closes the other two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.