For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > November 2005 > Sockets and keepalive









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sockets and keepalive
Francois Goudal

2005-11-14, 7:00 pm

Hi,
I'm trying to write a small daemon. It is a tcp server and so I use the
posix sockets but i get a problem that I can't solve...
Here is an extract of the code I use :

while (recv(client_socket, line, BUF_SIZE - 1, 0) > 0)
{
parse(line, client_socket, isauth);
memset(line, 0, BUF_SIZE);
}
client_disconnect(client_socket);

The problem is :
When a client properly disconnects, the client_disconnect procedure is
well called.
But, when the link between the computers get down (for example, signal
loss on a wireless connection) and don't come back, this code doesn't
seem to get a timeout. I though that once a time, recv would return a
negative value, indicating a timeout but it seems that this doesn't
occurs, even if I wait 2 hours...
The problem is that I would like to detect this quite quickly cause it
would impact the billing of the users of this software so I need to
detect as soon as possible that the link is no more active, in order to
close the socket.

I heared about SO_KEEPALIVE but it seems that the timeout is 2 hours or
more so it is too long...
I also tried to send every 5 seconds some datas to the client, hoping
that the send method fails but without success...

So I would like to know if someone has any idea to get my problem solved.

Thank's

--
Francois Goudal
Epita promo 2008 - Ing1 - Tresorier Evolutek
francois@goudal.net
David Schwartz

2005-11-14, 7:00 pm


"Francois Goudal" <francois_nospam_@goudal.net> wrote in message
news:4378bf6e$0$21720$626a54ce@news.free.fr...

> Hi,
> I'm trying to write a small daemon. It is a tcp server and so I use the
> posix sockets but i get a problem that I can't solve...
> Here is an extract of the code I use :
>
> while (recv(client_socket, line, BUF_SIZE - 1, 0) > 0)
> {
> parse(line, client_socket, isauth);
> memset(line, 0, BUF_SIZE);
> }
> client_disconnect(client_socket);


How can this code work since:

1) You don't know how many bytes you recevied.

2) You don't check if you received a complete line before you parse it.

> The problem is :
> When a client properly disconnects, the client_disconnect procedure is
> well called.
> But, when the link between the computers get down (for example, signal
> loss on a wireless connection) and don't come back, this code doesn't seem
> to get a timeout. I though that once a time, recv would return a negative
> value, indicating a timeout but it seems that this doesn't occurs, even if
> I wait 2 hours...


Are you designing the protocol or implementing an existing procotol (on
top of TCP)? At the implementation phase, you should follow the protocol
specification for how to handle this condition. At the design phase, you
should ensure that either:

1) Each side sends periodically. TCP is guaranteed to detect a
connection loss on a side that is sending.

2) Timeouts are built into the protocol. That is, it is not legal for a
side that is not sending to not receive anything for more than a specified
amount of time.

There are any number of ways to assure this, depending on the protocol.

Is this your own protocol on top of TCP? Or are you implementing an
existing one? If an existing one, which?

DS


Alexander Baranov

2005-11-16, 7:02 pm


"Francois Goudal" <francois_nospam_@goudal.net> wrote in message
news:4378bf6e$0$21720$626a54ce@news.free.fr...
> Hi,
> I'm trying to write a small daemon. It is a tcp server and so I use the
> posix sockets but i get a problem that I can't solve...
> Here is an extract of the code I use :
>
> while (recv(client_socket, line, BUF_SIZE - 1, 0) > 0)
> {
> parse(line, client_socket, isauth);
> memset(line, 0, BUF_SIZE);
> }
> client_disconnect(client_socket);
>
> The problem is :
> When a client properly disconnects, the client_disconnect procedure is
> well called.
> But, when the link between the computers get down (for example, signal
> loss on a wireless connection) and don't come back, this code doesn't
> seem to get a timeout. I though that once a time, recv would return a
> negative value, indicating a timeout but it seems that this doesn't
> occurs, even if I wait 2 hours...
> The problem is that I would like to detect this quite quickly cause it
> would impact the billing of the users of this software so I need to
> detect as soon as possible that the link is no more active, in order to
> close the socket.
>
> I heared about SO_KEEPALIVE but it seems that the timeout is 2 hours or
> more so it is too long...
> I also tried to send every 5 seconds some datas to the client, hoping
> that the send method fails but without success...
>
> So I would like to know if someone has any idea to get my problem solved.

Hi, I had similar problem. You have to make your socket non-blocking. Then
if the connection breaks and you try to receive your receiver will return
e.g -1 (I don't remember exactly)
Regards, Alex.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com