If only it were that straightforward...
The sysread will block indefinitely if the far end simply stops talking. This is not a common case, but can be achieved if the far end falls of the network for some reason (or spontaneously combusts). It is just possible for this to happen between the connection being established and the read loop being entered -- so you are best off with the $select->can_read at the head of the loop.
The $select->can_read will return in the event of an error, and the sysread will detect the error and return undef, and $! contains the error. The only soft errors I know of are: (a) EAGAIN, but that should only occur if the socket is non-blocking -- and should not occur after a can_read; and (b) EINTR. which can happen if you have signals (eg SIGALRM) going off -- but generally those will hit the $select->can_read, causing an early time-out.
So the code should distinguish: (a) time-out, (b) data read, (c) eof, (d) soft error (if any) and (e) hard error. I would do something along these lines:
(assuming $select is set up for the $sock only.)my $buffer = '' ; my $rc ; while (1) { if ($select->can_read(1)) { $rc = sysread($sock, $buffer, 64*1024, length($buffer)); next if $rc ; # If any chance of a soft error... eg: # if (!defined($rc)) { # next if $! == EAGAIN ; # Not really expected, even with non +-blocking # next if $! == EINTR ; # Signals ! # } ; } else { # If signals floating around, can reach here if a signal goes of +f... $rc = -1 ; last ; } ; } ; # Now: $rc == 0 => no error, == -1 => timed out, == undef => error
NB: only if you use non-blocking IO do you need to worry about EAGAIN, and then only on the basis of paranoia. You only need to worry about EINTR if you have signals going off. What I haven't covered is how to distinguish real time-outs from early exit from $select->can_read caused by signals. Just before each $select->can_read you can my $time_out = time() + 1 (or whatever the time-out is) and test for $time_out < time() -- I suggest Time::HiRes for fewer surprises here. Alternatively, you could $! = 0 just before the $select->can_read and test for $! == EINTR.
In any event, brother ikegami is correct, it would be a good idea to have some way of telling that what you receive is complete. When sysread returns 0 it means there is no more data and the connection has been closed -- this in no way guarantees that everything was sent before the connection was closed... something else may have happened to cause that !
In reply to Re^4: sysread and syswrite in tcp sockets
by gone2015
in thread sysread and syswrite in tcp sockets
by rustybar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |