Foo Ji-Haw wrote:
> Hi all,
>
> Thanks to John and Bill, I've managed to get my socket disconnect
> detection working.
> I'd like to share my check code with all:
> my $BytesRead = $sock->sysread($buffer,1024);
> if (!defined($BytesRead))
> {
> print "WARNING: Connection lost!\n";
> exit;
> }
I use something like this:
sub read_sock { # my $bytes = read_sock ($S, \$buffer, [$bufoff, [$buflen]])
my $sock = shift;
my $bufref = shift;
my $off = shift || 0; # you can reverse these two args if you
like
my $len = shift || MAX_READ; # 4096/8192 whatever is appropriate for
your data
my $bytes = sysread $sock, $bufref, $len, $off;
if (not defined $bytes or $bytes < 0) { # read error
return -1;
} elsif ($bytes == 0) { # eof
return 0;
}
return $bytes;
}
> My next obstacle is with sysread blocking. I tried to do the following:
> my $sock = new IO::Socket::INET (
> PeerAddr => $ARGV[0],
> PeerPort => '8000',
> Proto => 'tcp',
> Reuse=>1,
> );
> $sock->blocking(0);
>
> But it still blocks. Browsing the web, David Smith posted the following
> code:
>
> my $select = = IO::Select->new();
> $select->add( $socket );
> if( $select->can_read( 0.5 ) )
> {
> $response = <$socket>;
You want to call the read sub above here and check the return for
err/eof/bytes. Don't use <> syntax.
> print $response . "\n";
> } else
> {
> print "No data on socket.\n";
> }
>
> I'd like to ask if using IO::Select is the best/ only way to do a
> non-blocking sysread(), or am I missing something?
IO::Select is the best way to see if you have data to read on a
socket. sysread is the way to read it.
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs