How to reconnect the clients to server?

2020-07-19 15:24发布

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?

5条回答
够拽才男人
2楼-- · 2020-07-19 15:30

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?

查看更多
家丑人穷心不美
3楼-- · 2020-07-19 15:36
int
connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen)
{
    int nsec;

    /*
     * Try to connect with exponential backoff.
     */

    for (nsec = 1; nsec <= MAXSLEEP; nsec <<= 1) {
        if (connect(sockfd, addr, alen) == 0) {

            /*
             * Connection accepted.
             */

            return(0);
        }

        /*
         * Delay before trying again.
         */

        if (nsec <= MAXSLEEP/2)
            sleep(nsec);
    }
    return(-1);
}

refered by Advanced programming in the unix Environment book.

You can also use:

SO_REUSEADDR in setsockopt(). It allows the reuse of local addresses.

查看更多
forever°为你锁心
4楼-- · 2020-07-19 15:49

Let me start by saying that anything is possible. There is a function which does that for you. It is the same connect that you might have used for your TCP client. You just need to think about when you need to call this connect again.

So when do I use that connect function now?

Let me present one possible solution.

You need to have some sort of monitoring software (maybe a daemon) which keeps track of the state of the server process. It could, say, periodically poke the server process to see if it's alive.

Consider the case of a single client and server. The client is running on system A; the server, on system B.

Say the server has run and crashed right before it recved anything. This means the client would have successfully connected to the server and its send will fail. When the send fails, you can contact your monitoring software on system B to see what happened.

If the monitoring software reports that it didn't find anything wrong with the server, something else went wrong then (possibly an interrupt, your NIC went kaput, whatever). Those reasons are outside the scope of this discussion.

If your monitoring software replies saying that it found that the server program died, then you can:

  • reply to the monitoring software asking it to start the server again
  • OR tell it to shut itself down
  • OR do something else that you see fit.

Now, in the client in system A, begin the process of socket, connect, send, recv, etc again.

Essentially, you are creating another server, X, which looks after your current server, Y. When server Y dies, you look to server X for reasons.

查看更多
叛逆
5楼-- · 2020-07-19 15:50

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..

查看更多
疯言疯语
6楼-- · 2020-07-19 15:55

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):

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

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

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

and then start over beginning with:

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

Starting over and beginning with:

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

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

查看更多
登录 后发表回答