$socket->timeout usage

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.
 
S

Sébastien Cottalorda

A. Sinan Unur said:
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.

Hi,

To manage timeout on socket use IO::Select package (see perldoc).
To not use the buffered socket use the $|=1; command at the beginning of
your script.

Cheers.

Sébastien
 
A

Anno Siegel

Sébastien Cottalorda said:
Hi,

To manage timeout on socket use IO::Select package (see perldoc).

Well, yes, that's a possibility.

However, if I understand Sinan's question, it is why the secondary
socket doesn't seem to time out according to the timeout parameter.

I don't know either. Socket timeouts have puzzled people for ages.
To not use the buffered socket use the $|=1; command at the beginning of
your script.

That won't help, unless you select() (the other "select") the socket
first.

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
Well, yes, that's a possibility.

However, if I understand Sinan's question, it is why the secondary
socket doesn't seem to time out according to the timeout parameter.

Yes, that is indeed the question. Thank you Anno.
I don't know either. Socket timeouts have puzzled people for ages.

OK :)

Sebastien, according to perldoc IO::Socket:

As of VERSION 1.18 all IO::Socket objects have autoflush turned on by
default.

Further, the example script is not using sockets for output at all so
autoflush behavior is very likely irrelevant.

I am curious about this. I would have been less puzzled if the second
timeout were ignored in Windows and respected in FreeBSD (after all, even
alarm works in certain contexts in Windows).

Thanks.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top