sockets - Java SocketException Broken pipe -
i doing client server log-in communication.
i met java.net.socketexception: broke pipe @ server end. , have narrowed problem one single line @ client end. if move position line, code works. plese see following code.
client end:
socket socket = new socket(const.destip, 12101); objectoutputstream out = new objectoutputstream(socket.getoutputstream()); out.writeobject(this.message); out.close();//line cause problem objectinputstream in = new objectinputstream(socket.getinputstream()); servertoclientlogin msg = (servertoclientlogin) in.readobject(); //out.close();//move 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 , writeobject blocking call, have no idea why close earlier case such problem.
out.close();
: closes (out
) output stream , releases system resources associated stream.
see api here.
Comments
Post a Comment