三、Perl语言实现的socket超时设置
Perl同样也有对connect, send/recv的不同超时设置。
下面的示例代码对这两种超时都做了设置,并每一种socket操作都做最多五次重试。
#! /usr/bin/perl
###############################################################################
# \File
# tcp_client.pl
# \Descript
# send message to server
###############################################################################
use IO::Socket;
use IO::Select;
my $ip = shift;
#hash to install IP Port
%srv_info =(
"srv_ip" => "192.168.1.73",
"srv_port"=> "8080",
);
my $srv_addr = $srv_info{"srv_ip"};
my $srv_port = $srv_info{"srv_port"};
my $sock;
foreach(0..4)
{
$sock = IO::Socket::INET->new(
PeerAddr => "$srv_addr",
PeerPort => "$srv_port",
Type => SOCK_STREAM,
Blocking => 1,
Timeout => 2,
Proto => "tcp");
if ($sock)
{
last;
}
else
{
print "Connect timeout\n";
}
}
if (!$sock)
{
print "Cann't connect to server\n";
exit;
}
my $timeout = 2;
my $sel = IO::Select->new($sock);
foreach(0..4)
{
my $fh_write = $sel->can_write($timeout);
if ($fh_write)
{
$sock->send("Hello server!\n", 0) or warn "send failed: $!, $@";
$sock->autoflush(1);
last;
}
else
{
print "Send timeout!\n";
}
}
-
foreach(0..4)
-
{
-
my $fh_read = $sel->can_read($timeout);
-
if ($fh_read)
-
{
-
my $rcv = <$sock>;
-
print "RCV : ",$rcv, "n";
-
last;
-
}
-
else
-
{
-
print "Recv timeoutn";
-
}
-
}
$sock->close();
exit;
用九,见群龙无首,吉。
【白话】用九,出现群龙也不愿意为首的现象,是很吉利的。
《象》曰:“用九”,天德不可为首也。
【白话】《象辞》说:“用九”的爻象说明,天虽生万物,但却不居首、不居功。
阅读(736) | 评论(0) | 转发(0) |