1
2
3
4
5
6
7
eval{
local$SIG{ALRM} = sub{ die"alarm time out"};
alarm$TIMEOUT;
$sock->recv($msg, $MAXLEN)      or die"recv: $!";
alarm0;
1;  # return value from eval on normalcy
} or die"recv from $server_host timed out after $TIMEOUT seconds.\n";

#定义ALRM发生信号时的操作,这里发信给客户端并断开链接,子进程退出
$SIG{ALRM} = sub {
print "recv timeout\n";
##syswrite($session,"timeout");
my $bytes=send($session,"timeout",0);
&log($bytes);
close($session);
exit();
};

alarm( 30 ); #同上,一般放在会超时的操作代码
my $bs=recv($session,$buf,2048,0);
&log($bs);
alarm( 0 );