Roman
Roman

Reputation: 862

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.SocketException: Broken pipe

despite the fact that

socket.isClosed - false
socket.isOutputShutdown - false
socket.isConnected - true

Upvotes: 0

Views: 693

Answers (2)

user207421
user207421

Reputation: 310915

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.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

It 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.

Upvotes: 3

Related Questions