Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How do i detect that the client has disconnected ? (i'm using sysread/write)

( #116051=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie shlomi on Oct 02, 2001 at 06:04 UTC
Q&A  > network programming


Description:

i tried something like this : this did not work : my $written = syswrite($sock, $buf, 1024); if (not defined $written) { close_sock($sock); }

Answer: How do i detect that the client has disconnected ? (i'm using sysread/write)
contributed by pjf

You can use getpeername() to reliably tell if your socket is connected or not, if it returns undef, you don't have a connection. This is exactly how IO::Socket handles things.

The code that you've written should work, although it will close the socket on any connection, not just the connection going away. In particular, if your socket is in non-blocking mode, syswrite can return errors (EAGAIN) if the other side simply isn't ready for more data.

If you are using non-blocking connections, or multiple connections at once, then the chances are you'll find the built-in function select, or the module IO::Select, very handy for telling when it's time to read/write to your sockets. The Perl Cookbook has a good example of this in section 17.13 (Non-Forking Servers).

Hope you find the above useful. Let me know if it doesn't solve your problem.

Cheers,
Paul

Answer: How do i detect that the client has disconnected ? (i'm using sysread/write)
contributed by entropy

The select() function will tell you when there is data waiting to be read on a socket. If the connection is lost, select() will lie and say there is something to be read. When you try to read the line, sysread will return undef.

my $bit_in = ''; my $r_bit; vec($bit_in, fileno($f), 1) = 1; while(1) { select($r_bit = $bit_in, undef, undef, tick_len); # if select said there was data... if(vec($r_bit,fileno($f),1)) { my $in; # connection lost if sysread returns undef return unless $f->sysread($in, 1); # otherwise, it's good data $str .= $in; last if $in eq "\n"; } }
Answer: How do i detect that the client has disconnected ? (i'm using sysread/write)
contributed by Zombie shlomi

Hello Paul, Thank you for the reply . I did some more reading about syswrite in perldoc but there's nothing about returning errors except the undef value (EAGAIN??). I don't want to use "IO::Whatever" , I want to do this using only low level functions. My goal is a low level server, non-blocking, non-forking ,multiple connections ... its working , but i have this problem with the detection of disconnected sockets. can you please give me a short example of using getpeername for detection ? thanks, Shlomi

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 musing on the Monastery: (6)
    As of 2014-07-30 21:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (241 votes), past polls