I write a simple echo server and client script to test socket timeout. Originally, I think timeout in IO::Socket::INET means the sysread/syswrite will return a ETIMEOUT error if it can't read/write the message within the duration of the timeout. But from the result of my test, it seems that I am very wrong about this. Following is my simple code:
sever:client:#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; my $listenSocket = IO::Socket::INET->new( 'LocalPort' => '12345', 'Listen' => SOMAXCONN, 'Reuse' => 1, 'Proto' => 'tcp', ) or die $@; my $conSocket = $listenSocket->accept; while ( defined $conSocket ) { my $rsp = q{}; $conSocket->sysread( $rsp, 20 ) or die $@; print "recv: $rsp\n"; $conSocket->syswrite( "hello, client!\n" ) or die $@; } ## end while ( defined $conSocket)
#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; my $socket = IO::Socket::INET->new( 'Proto' => 'tcp', 'PeerAddr' => '172.16.249.232', 'PeerPort' => '12345', ) or die $@; $socket->timeout(2); while (1) { $socket->syswrite( "hello, server!\n") or die $!; my $rsp = q{}; $socket->sysread( $rsp, 20 ) or die $!; print "recv:$rsp\n"; sleep 1; } ## end while (1)
I run the server side script first, and then run the client side script, when the connection is established, and confirm that the two side can communicate with each other, then I disconnect the client side from the network, and wait for 2 seconds, but the client side didn't complain a ETIMEOUT error:(. So I think I misunderstood the meanning of the timeout in IO::Socket::INET.
Can anyone explain what does timeout means in socket? Thanks in advance.
In reply to what does timeout mean in IO::Socket::INET ? by sunshine_august
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |