java – How can I tell if a socket is "listening" on the server side?

Question:

When I use java.net.Socket isConnected() function on the client side and it returns true can I make sure the server is "listening"? Otherwise how can I know? Just a wait time between establishing a connection and starting "writing"?

PS: I put the Android tag because I don't know if it has influence.

Answer:

Good evening Jorge,

As the documentation says: Note: Closing a socket doesn't clear its connection state, which means this method will return true for a closed socket (see isClosed()) if it was successfuly connected prior to being closed.

When a socket connection closes it does not change the object's state, so it remains true.

The best approach is for you to implement an ACK on your client/server. Your client ACKs your server and it responds. If there is an error writing on the socket you know that the connection was lost and connect again.

abs

Scroll to Top