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


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

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 Re: How do i detect that the client has disconnected ? (i'm using sysread/write)
  • Download Code