======================================================================
#!/usr/bin/perl
# FILE: try_socket_perl_monks.pl
#
use warnings;
use strict;
print "PROGRAM: try_socket_perl_monks.pl \$ARGV[0]=$ARGV[0]\n";
my $MySocket; # socket
my $t; # time
use IO::Socket;
if ($ARGV[0] eq "A")
{
$MySocket=new IO::Socket::INET->new(Proto => 'udp',
PeerAddr=> '192.168.22.30',
PeerPort=>8005,
LocalAddr=> '192.168.21.28',
LocalPort=>8006);
print "Configuration A, \$MySocket=$MySocket\n";
}
elsif($ARGV[0] eq "B")
{
my $MySocket=new IO::Socket::INET->new(Proto => 'udp',
PeerAddr=> '192.168.21.28',
PeerPort=>8006,
LocalAddr=> '192.168.22.30',
LocalPort=>8005);
print "Configuration B, \$MySocket=$MySocket \n";
}
elsif($ARGV[0] eq "C")
{
my $MySocket=new IO::Socket::INET->new(Proto => 'udp',
PeerAddr=> '192.168.20.223',
PeerPort=>8006,
LocalAddr=> '192.168.21.28',
LocalPort=>8005);
print "Configuration C, \$MySocket=$MySocket \n";
}
else
{
print "Passed argument error \n";
}
if (defined($MySocket))
{
print "\$MySocket = $MySocket\n";
}
else
{
print "\$MySocket not defined\n";
}
my $msg = "This is a test message 12345678901234567890123456789012345678901234567890\n";
$MySocket->send($msg);
$MySocket->send($msg);
$MySocket->send($msg);
$MySocket->send($msg);
print "After the sending of the message\n";
my $text;
my $more = 1;
my $retries = 0;
do
{
eval {
local $SIG{ALRM} = sub{++$retries and die "timeout\n" };
alarm(3);
$retries++;
print "above the recv, \$retries=$retries\n";
$MySocket->recv($text, 128) or die "recv failed: $!";
alarm(0);
print "\$retries=$retries, length(\$text)=" . length($text) . "\n";
};
print "retrying \$retries=$retries\n";
} while $retries < 4;
print "END OF PROGRAM\n\n";
####
PROGRAM: try_socket_perl_monks.pl $ARGV[0]=A
Configuration A, $MySocket=IO::Socket::INET=GLOB(0x818416c)
$MySocket = IO::Socket::INET=GLOB(0x818416c)
After the sending of the message
above the recv, $retries=1
retrying $retries=2
above the recv, $retries=3
retrying $retries=4
END OF PROGRAM
####
PROGRAM: try_socket_perl_monks.pl $ARGV[0]=B
Configuration B, $MySocket=IO::Socket::INET=GLOB(0x81842e4)
$MySocket not defined
Can't call method "send" on an undefined value at ./try_socket_perl_monks.pl line 59
####
PROGRAM: try_socket_perl_monks.pl $ARGV[0]=C
Configuration C, $MySocket=IO::Socket::INET=GLOB(0x81842e4)
$MySocket not defined
Can't call method "send" on an undefined value at ./try_socket_perl_monks.pl line 59