For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2007 > How to identify if the client is still connected









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 How to identify if the client is still connected
srikanthks.hsr@gmail.com

2007-07-17, 7:07 pm

Hi,
I have a problem with a client server application. I have one server
which connects with say 10 clients. I want to know if any one of the
client shutdown gracefully closing the socket OR a network cable was
unplugged.
What is the best way to find out the presence of a client. I do not
want to send a dummy packet to the client and wait for a SIGPIPE error
to know that the client has closed the socket graecfully.
Can you help me with a solution for this problem?
Regards
Sri

Alex Fraser

2007-07-17, 7:07 pm

<srikanthks.hsr@gmail.com> wrote in message
news:1184683378.187881.244950@d30g2000prg.googlegroups.com...
> I have a problem with a client server application. I have one server
> which connects with say 10 clients. I want to know if any one of the
> client shutdown gracefully closing the socket OR a network cable was
> unplugged.


For stream sockets (eg TCP), graceful shutdown by the peer is indicated by
the socket becoming readable, and a read() or recv() call on the socket will
return 0 after all preceding data have been read.

In general there is no notification if connectivity is lost, such as a
network cable being unplugged. Sometimes you will get an error (EPIPE or
ECONNRESET) or signal (SIGPIPE) if you call a socket function, but it
depends on the cirumstances.

If it is necessary to detect loss of connectivity or other communication
problems, as is imperative in a server, you must be able to infer it from a
timeout. That is, you must detect that it has been too long since you
managed to send data, or too long since you expected to receive data.

The details, such as how long "too long" is, depend on the application. You
didn't describe your application, but I guess it is one where the client and
server may not have anything useful to say to each other for an arbitrarily
long period of time. If so, the only way you can practically use a timeout
is to introduce a protocol exchange that has no effect apart from resetting
the timer. For example, IRC servers send periodic "PING" messages to
clients, and if no "PONG" message is received in a reasonable time), the
server concludes that something is wrong and closes the connection.

Alex


Sponsored Links







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

Copyright 2008 codecomments.com