How to reconnect the clients to server?

16,757

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 server
read/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.

Share:
16,757
loganaayahee
Author by

loganaayahee

Updated on July 05, 2022

Comments

  • loganaayahee
    loganaayahee almost 2 years

    My server program (socket stream) is running and it accepts the clients. Due to some abnormal condition, server is getting terminated. The other side clients are waiting for server reply. How to I reconnect the running clients to new server? any functions in sockets?

  • alk
    alk about 11 years
    Isn't the OP talking about the case, that the server goes down after having successfully connected?
  • user207421
    user207421 about 4 years
    What exactly can server X do that the client can't? And what's wrong with an ordinary read timeout reading the response, and no server X at all?
  • user207421
    user207421 about 4 years
    This code does not work. You can't reconnect a TCP socket, even if the prior connect attempt failed. You have to close it and create a new one. Don't post untested code techniques here.
  • Anish Ramaswamy
    Anish Ramaswamy about 4 years
    Not saying you can't do that. The trade-off is if that client dies or is compromised integrally you'd need to handle that. Also, if your devops team suddenly decides to re-provision another server located elsewhere, will you issue a client update (which potentially costs money depending on platform)? There are trade-offs to every situation.
  • user207421
    user207421 about 4 years
    You're over-complicating a very simple situation. Every network client ever written needs a read timeout facility. You can't write magical code in server X: you can only write code that should have been in the clients in the first place. Read timeout is one line of code to set and one catch block to detect. This is very basic.
  • Anish Ramaswamy
    Anish Ramaswamy about 4 years
    Repeating your earlier statement in a condescending tone isn't going to change what I said. I am not at all saying you can't or shouldn't do client read-timeouts. All I am saying is that there are trade-offs to both our solutions. I also explicitly stated that you can simply reconnect right in my first paragraph.