Should I close sockets from both ends?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-16T13:15:48Z Indexed on 2010/03/16 13:16 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

I have the following problem. My client program monitor for availability of server in the local network (using Bonjour, but it does not rally mater). As soon as a server is "noticed" by the client application, the client tries to create a socket: Socket(serverIP,serverPort);.

At some point the client can loose the server (Bonjour says that server is not visible in the network anymore). So, the client decide to close the socket, because it is not valid anymore.

At some moment the server appears again. So, the client tries to create a new socket associated with this server. But! The server can refuse to create this socket since it (server) has already a socket associated with the client IP and client port. It happens because the socket was closed by the client, not by the server. Can it happen? And if it is the case, how this problem can be solved?

Well, I understand that it is unlikely that the client will try to connect to the server from the same port (client port), since client selects its ports randomly. But it still can happen (just by chance). Right?

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets