A
A. Sinan Unur
Consider the following script:
#!/usr/bin/perl
use strict;
use warnings;
use Socket;
use IO::Socket;
my $s = IO::Socket::INET->new(
LocalAddr => '127.0.0.1',
LocalPort => 50000,
Reuse => 1,
Listen => 5,
Timeout => 10,
);
die "$@" unless $s;
while(my $c = $s->accept()) {
$c->timeout(5);
while($c->getline()) {
print;
}
$s->shutdown(2);
}
print $@ if $@;
__END__
Now, the 10 second timeout I set on $s works like I expected: If a
connection does arrive in 10 seconds, accept fails with:
C:\develop\perl> sto.pl
accept: timeout
on both Windows XP and FreeBSD 4.8. OTOH, if I connect using
telnet 127.0.0.1 50000
before the accept call times out, the script just hangs waiting for input
and never times out.
Changing the while loop to:
my ($byte, $msg);
while(sysread($c, $byte, 1) == 1) {
last if $byte eq 'z';
$msg .= $byte;
print $msg, "\n";
}
does not make a difference either.
I do realize this is a naive query and I am probably missing something
obvious. I'd really appreciate some pointers.
Sinan.
#!/usr/bin/perl
use strict;
use warnings;
use Socket;
use IO::Socket;
my $s = IO::Socket::INET->new(
LocalAddr => '127.0.0.1',
LocalPort => 50000,
Reuse => 1,
Listen => 5,
Timeout => 10,
);
die "$@" unless $s;
while(my $c = $s->accept()) {
$c->timeout(5);
while($c->getline()) {
print;
}
$s->shutdown(2);
}
print $@ if $@;
__END__
Now, the 10 second timeout I set on $s works like I expected: If a
connection does arrive in 10 seconds, accept fails with:
C:\develop\perl> sto.pl
accept: timeout
on both Windows XP and FreeBSD 4.8. OTOH, if I connect using
telnet 127.0.0.1 50000
before the accept call times out, the script just hangs waiting for input
and never times out.
Changing the while loop to:
my ($byte, $msg);
while(sysread($c, $byte, 1) == 1) {
last if $byte eq 'z';
$msg .= $byte;
print $msg, "\n";
}
does not make a difference either.
I do realize this is a naive query and I am probably missing something
obvious. I'd really appreciate some pointers.
Sinan.