Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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

by Zombie shlomi (Initiate)
on Oct 02, 2001 at 06:04 UTC ( #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


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.


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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [Cosmic37]: hello perly kings and queens
    [jedikaiti]: 'ello Monks
    [Cosmic37]: and monkeys too...
    [Cosmic37]: is anyone doing machine learning with Perl?
    [Cosmic37]: I was using gneural code in C and it was interesting but did not have back propagation for neural networks
    [corenth]: Cosmic, poking at it, but not really.
    [Cosmic37]: I read that its possible to use the FANN library with Perl so I might try that now...
    [corenth]: i have a question. I used map{ blah();blah(); blah();}@stuff; and it used up a ton of memory vs. the for(@stuff){} equivalent. What gives? (if anyone knows)
    [Cosmic37]: FANN also has LGPL license which I like and its supposed to be quite a capable library from what I read
    [corenth]: Cosmic, that sounds interesting. What is FANN (I could search it if I weren't so lazy about it)?

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (7)
    As of 2018-02-20 18:15 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (274 votes). Check out past polls.