Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

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

( #116858=categorized answer: print w/ replies, xml ) Need Help??

Q&A > network programming > 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"; } }

Comment on Answer: How do i detect that the client has disconnected ? (i'm using sysread/write)
Download Code
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 cooling their heels in the Monastery: (12)
As of 2014-04-23 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (548 votes), past polls