in reply to IO::Select->can_read isn't recognising closed INET connections

When a client's INET socket is closed, the socket on the server's end will show up as 'ready', but the next read from it will read 0 bytes, or in perl's case as 'undef' if you read it with <$socket>. In the case of 'non-blocking' io, if you get a positive can_read() and read length of 0, then the socket was closed on the other end.
@reads = $select->can_read() ; for( @reads ) { $data = <$_> ; if( $data == undef && $! != EWOULDBLOCK ) { # socket closed from the other end } }
Reference: Unix Network Programming 2nd Ed Vol 1 page 166-167, by W. Richard Stevens
  • Comment on Re: IO::Select->can_read isn't recognising closed INET connections
  • Download Code