How to reconnect the clients to server? How to reconnect the clients to server? unix unix

How to reconnect the clients to server?


A socket that had been connect()ed once cannot be reused with another call to connect().

The steps to connect to a TCP server and read/write some data are as follows (pseudo code):

sd = socket(...) // create socket descriptor (allocate socket resource)connect(sd, server-address, ...) // connect to serverread/write(sd, data)  // read from server close(sd) // close /socket descriptor (free socket resource)

In case the server goes down after connect all the client could and shall do is

close(sd) // close socket descriptor (free socket resource)

and then start over beginning with:

sd = socket(...) // create socket descriptor (allocate socket resource)...

Starting over beginning with:

connect(sd, server-address, ...) // connect to server...

would probably lead to undefined behaviour, but at least to an error.


You connect to the new server the same way you connected to the original server. There isn't a different API for this. I don't see why you would think otherwise..


You cant handle this in server but you can create a session for your clients then when your client reconnect restore their settings and continue on for sending and receiving messages and in your client side application create a thread with specific interval to check if the server is available or not and if so try a reconnect procedure but, I suggest you to check your server side program what happens that your program goes down?