How to I reconnect the running clients to new server

Ask Question

up vote3down votefavorite

1

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?

c sockets unix network-programming

shareimprove this question

edited Apr 25 '13 at 9:58

saeed

2,06511437

asked Apr 25 '13 at 9:23

loganaayahee

614513

show 2 more comments

5 Answers

activeoldestvotes

up vote1down voteaccepted

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

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

fd = socket(...) // create socket describtor (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 describtor (allocate socket resource)
...

starting over and beginning with:

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

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

shareimprove this answer

edited Apr 25 '13 at 12:19

answered Apr 25 '13 at 12:13

alk

56.5k758161

add a comment

 

up vote1down vote

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.

shareimprove this answer

edited Apr 25 '13 at 9:52

Anish Ramaswamy

1,50021553

answered Apr 25 '13 at 9:43

Vidya

526614

add a comment

up vote1down vote

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

shareimprove this answer

edited Apr 25 '13 at 12:54

answered Apr 25 '13 at 10:46

Anish Ramaswamy

1,50021553

add a comment

up vote0down vote

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

shareimprove this answer

answered Apr 25 '13 at 9:44

user207421

255k23202341

add a comment

up vote0down vote

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?

shareimprove this answer

answered Apr 25 '13 at 9:55

saeed

2,06511437

add a comment

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值