How can I force the server socket to re-accept a request from a client?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-17T11:13:17Z Indexed on 2010/03/17 11:41 UTC
Read the original article Hit count: 203

Filed under:
|
|

For those who does not want to read a long question here is a short version: A server has an opened socket for a client. The server gets a request to open a socket from the same client-IP and client-port. I want to fore the server not to refuse such a request but to close the old socket and open a new one. How can I do ti?


And here is a long (original) question:

I have the following situation. There is an established connection between a server and client. Then an external software (Bonjour) says to my client the it does not see the server in the local network. Well, client does nothing about that because of the following reasons:

  1. If Bonjour does not see the server it does not necessarily means that client cannot see the server.

  2. Even if the client trusts the Bonjour and close the socket it does not improve the situation ("to have no open socket" is worser that "to have a potentially bad socket").

So, client do nothing if server becomes invisible to Bonjour. But than the server re-appears in the Bonjour and Bonjour notify the client about that. In this situation the following situations are possible:

  1. The server reappears on a new IP address. So, the client needs to open a new socket to be able to communicate with the server.

  2. The server reappears on the old IP address. In this case we have two subcases:

2.1. The server was restarted (switched off and then switched on). So, it does not remember the old socket (which is still used by the client). So, client needs to close the old socket and open a new one (on the same server-IP address and the same server-port).

2.2. We had a temporal network problem and the server was running the whole time. So, the old socket is still available for the use. In this case the client does not really need to close the old socket and reopen a new one.

But to simplify my life I decide to close and reopen the socket on the client side in any case (in spite on the fact that it is not really needed in the last described situation).

But I can have problems with that solution. If I close the socket on the client side and than try to reopen a socket from the same client-IP and client-port, server will not accept the call for a new socket. The server will think that such a socket already exists.

Can I write the server in such a way, that it does not refuse such calls. For example, if it (the server) sees that a client send a request for a socket from the same client-IP and client-port, it (server) close the available socket, associated with this client-IP and client-port and than it reopens a new socket.

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets