I would have assumed that calling isConnected()
on a socket would tell me whether if is connected to the other side or not.
Wrong. It tells you whether you ever connected this Socket. It doesn't tell you about the state of the connection.
Returns: true if the socket successfuly connected to a server
Note that it doesn't say 'is currently connected' to a server.
How is isConnected
different than isClosed,
and what is the real behavior of each?
The real behaviour of both is that they tell you what you have done to the socket, not what the state of the connection is.
How do I tell if the other side if officially closed without writing anything to the streams, or creating new connections? Is there even a way?
No there isn't. If the peer closed normally, a read will return an EOS indication (read()
returns -1, readLine()
retuns null, readXXX()
for any other XXX throws EOFException
). A write will throw an IOException
'connect reset' or 'broken pipe' depending on your platform. TCP doesn't support anything in the nature of a 'dial tone', so absent a pending write there is no current connection status to enquire on.