Wenn Sie den Code überprüfen Sie werden sehen (Ich kopierte es von meinem Ubuntu 10.04):
my $timeout = ${*$sock}{'io_socket_timeout'};
# my $before = time() if $timeout;
undef [email protected];
if ($sock->connect(pack_sockaddr_in($rport, $raddr))) {
# ${*$sock}{'io_socket_timeout'} = $timeout;
return $sock;
}
return _error($sock, $!, [email protected] || "Timeout")
unless @raddr;
# if ($timeout) {
# my $new_timeout = $timeout - (time() - $before);
# return _error($sock,
# (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
# "Timeout") if $new_timeout <= 0;
# ${*$sock}{'io_socket_timeout'} = $new_timeout;
# }
Anscheinend ist die Timeout-Sachen so kommentiert, dass expleins warum wird es ignoriert.
Ich fand eine post aus dem Jahr 2003, wo dies diskutiert wurde. Ein Vorschlag (unten) war die Buchse in einem eval-Block zu öffnen, die von einem Alarmsignal beendet werden:
eval {
local $SIG{ALRM} = sub { die 'Timed Out'; };
alarm 3;
my $sock = IO::Socket::INET->new(
PeerAddr => inet_ntoa(gethostbyname($host)),
PeerPort => 'whois',
Proto => 'tcp',
## timeout => ,
);
$sock->autoflush;
print $sock "$qry\015\012";
undef $/; $data = <$sock>; $/ = "\n";
alarm 0;
};
alarm 0; # race condition protection
return "Error: timeout." if ([email protected] && [email protected] =~ /Timed Out/);
return "Error: Eval corrupted: [email protected]" if [email protected];
Nicht sehr elegant, aber wenn es funktioniert ...
Lassen Sie sich mit einem überprüfen langsame Server und ungeduldig Client:
# Impatient Client
use IO::Socket::INET;
$sock = new IO::Socket::INET(
PeerAddr => "localhost",
PeerPort => "10007",
Proto => 'tcp',
Timeout => 2,
);
print <$sock>;
close($sock);
# SlowServer
use IO::Socket::INET;
$sock = new IO::Socket::INET(
LocalAddr => "localhost",
LocalPort => "10007",
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
$newsock = $sock->accept();
sleep 5;
#while (<$newsock>) {
# print $_;
#}
print $newsock "Some Stuff";
close($newsock);
close($sock);
wenn wir führen dies:
[email protected]:~/playpen$ perl server.pl&
[1] 9130
[email protected]:~/playpen$ time perl test.pl
Some Stuff[1]+ Done perl server.pl
real 0m5.039s
user 0m0.050s
sys 0m0.030s
So ist es Igno Res die 2 Sekunden Timeout und läuft für die volle 5 Sekunden.
Nun ist die andere ungeduldig Client:
use IO::Socket::INET;
eval {
local $SIG{ALRM} = sub { die 'Timed Out'; };
alarm 2;
$sock = new IO::Socket::INET(
PeerAddr => "localhost",
PeerPort => "10007",
Proto => 'tcp',
Timeout => 2,
);
print <$sock>;
close($sock);
alarm 0;
};
alarm 0; # race condition protection
print "Error: timeout." if ([email protected] && [email protected] =~ /Timed Out/);
print "Error: Eval corrupted: [email protected]" if [email protected];
~
und es läuft:
[email protected]:~/playpen$ perl server.pl&
[1] 9175
[email protected]:~/playpen$ time perl test2.pl
Error: timeout.Error: Eval corrupted: Timed Out at test2.pl line 3.
real 0m2.040s
user 0m0.020s
sys 0m0.010s
Ja, das Timeouts nach 2 Sekunden, wie erwartet.
Das Attribut 'Timeout' wird nicht ignoriert, aber es wird im Konstruktor von' IO :: Socket' verwendet, nicht von 'IO :: Socket :: INET'. Dies ist jedoch eine gute Problemumgehung, und so etwas ist für Windows erforderlich. – mob
Das stimmt, aber es ist auskommentiert in der configure-Sub von IO :: Socket :: INET auskommentiert. AFAICS ist festgelegt, aber nie in :: INET verwendet. –
Danke, das hat für mich funktioniert. Aber, warum dieser Teil in der Quelle kommentiert ist? – code2be