http://www.perlmonks.org?node_id=73705

staticsea has asked for the wisdom of the Perl Monks concerning the following question:

I have a client/server application that I am working on, that both the client and the server have the capability of having a rather complicated conversation like an ftp client/server would.

The problem I have run into is that I have used a while loop on the client to listen for messages from the server, if the server daemon process dies, the client goes into an endless loop.

Is there a standard way of knowing that a socket connection has been lost on the client side?

I am using Perl 5.005_03 (on FreeBSD 4.3-RC#2), and IO-1.2, so I am not working with the latest and greatest, but I am forced to work within this framework for various reasons.

Originally posted as a Categorized Question.

  • Comment on How can I make a client realize the server connection is gone?

Replies are listed 'Best First'.
Re: How can I make a client realize the server connection is gone?
by zigster (Hermit) on Apr 19, 2001 at 12:16 UTC
    If the server closes the connection you should recieve an EOF on the socket. HOWEVER, tcp is designed to run over very slow and lossy networks, it is hard to tell the difference between a very slow/lossy network and a dead server.. In fact in tcp terms it is impossible. TCP works on a series of timeouts EVENTUALLY it will work out that the server is dead but it will take a long time.
    Having said all that things to consider.
    • Write a ping method into your server, get it to send a datagram every n seconds. If the client does not recieve that datagram then you know the server is dead.
    • Look at the socket option SO_KEEPALIVE this will implement a ping protocol for you, however its timeouts are huge. 2 Hours or more, you can change this time out on a persystem basis (exactly how depends upon the system), you cannot do this AFAIK on a per connection basis.

    For more information on this topic have a look at the UNIX Socket FAQ This document is as relivent to perl as it is to C, dont be put off by the fact that the examples are all in C. TCP is TCP is TCP.
Re: How can I make a client realize the server connection is gone?
by staticsea (Initiate) on Apr 19, 2001 at 23:20 UTC
    I found another solution after reading through the Unix Socket FAQ. At the beginning of the loop I do:
    my $error_code = $socket->sockopt(SO_ERROR);
    On FreeBSD this returns a value other than zero if there has been an error on the socket, so I am able to trap that and exit gracefully if an error occurs on the socket.
Re: How can I make a client realize the server connection is gone?
by dl748 (Initiate) on Jun 02, 2001 at 02:59 UTC
    If you are using a select to tell when you can read off of the connection, then you can tell the connection is closed becuase the select will say there is something to read off of it and when you read from it you will get 0 bytes read. Then You would close the connection. I've used this method for internal intranets and the internet.