|Just another Perl shrine|
Re: Detecting a closed socketby MarkM (Curate)
|on Aug 06, 2003 at 04:33 UTC||Need Help??|
Others have offered pieces of the puzzle. I'll try and offer you the complete solution:
Stream sockets (UNIX or INET) are bi-directional pipes. The pipe may be closed for reading or writing at either end (see shutdown() in perlfunc). When one end close()'s the socket, the pipe is closed for both reading and writing at that end.
If a socket is read from that was closed for writing at the remote end, the read system call will return 0 (see sysread() in perlfunc).
If a socket is written to that was closed for reading at the remote end, the write system call will return EPIPE (and the calling process may be forced to handle or ignore SIGPIPE).
IO::Select (really select() or poll()) should be used as an 'event ready' indicator, not a 'bytes' indicator.
In your latest response, you suggested that you would be verifying that write() returns the number of bytes that you expect it to. This is not proper, as the write system call may append the bytes to a system buffer, and return the number of bytes 'written' immediately. The bytes have not reached their destination at this point, and the socket could still close for reading at the remote end before your bytes reach it.
The subject of ensuring that records are transmitted successfully, completely, and that separate connections are synchronized is a complicated subject that I won't begin to discuss here.
Cheers, and good luck,