Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
Perl-Sensitive Sunglasses
 
PerlMonks  

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

( #73705=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie staticsea on Apr 19, 2001 at 04:18 UTC
Q&A  > network programming


Description:

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.

Answer: How can I make a client realize the server connection is gone?
contributed by zigster

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.
Answer: How can I make a client realize the server connection is gone?
contributed by Zombie staticsea

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.
Answer: How can I make a client realize the server connection is gone?
contributed by dl748

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.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (7)
    As of 2014-04-20 07:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (485 votes), past polls