Java socket second write attempt fails
I have a function:
...
socket.getOutputStream().write("something".getBytes());
socket.getOutputStream().flush();
...
Works fine. Keep socket open. Trying to call this function again but get the error: java.net.SocketExce开发者_高级运维ption: Broken pipe
despite the fact that
socket.isClosed - false
socket.isOutputShutdown - false socket.isConnected - trueIt is most likely the other end has closed the connection. It is possible the first write failed as well, as write() does not guarenteed delivery. You only get an Exception once it knows the other end is not listening.
isClosed
means; have I closed the connection
isOutputShutdown
means; have I shutdown the output
isConnected
means; has it ever connected
The only way to detect that a connection is truly up is to get a response from the other end telling you it has received your data. e.g. a response to a heartbeat. Without that response (which must be part of your protocol) you cannot be sure the other end has received it.
Broken pipe means that you wrote data to a connection that had already been closed by the other end. This can indicate an application protocol error.
精彩评论