UDP Client and Server
use IO::Socket;
$debug = 1;
sub say
{
my $i;
foreach $i (@_)
{
print "$i\n";
}
}
sub debug
{
my $i;
foreach $i (@_)
{
print "$i\n";
}
}
sub timeStamp
{
my ($sec,$min, $hour, $mday, $mon,$year,$wday,$yday,$isdst);
($sec,$min, $hour, $mday, $mon,$year,$wday,$yday,$isdst)=localtime(time);
$year+=1900;
$mon+=1;
return "$year $mon $mday $hour:$min:$sec ";
}
sub delWithConnection
{
debug 'ok: inter while, delWithConnection';
$receiveMessageChomp = chomp($receiveMessage);
say "ok: received: \$receiveMessageChomp: $receiveMessageChomp";
say "ok: received: \$receiveMessage: $receiveMessage";
}
$remoteIp = '127.0.0.1';
$remotePort = 999;
$localPort = 5599;
$messageSend = "echo test".timeStamp()."\n";
$local = IO::Socket::INET ->
new(LocalPort => $localPort,
Proto => 'udp',
Reuse => 1,
)
or die ("ERR: local Socket new");
$local -> autoflush();
$remote = IO::Socket::INET ->
new(PeerPort => $remotePort,
PeerAddr => $remoteIp,
Proto => 'udp',
Reuse => 1,
LocalPort => $localPort,
)
or die ("ERR: remote Socket new");
$remote -> autoflush();
print $remote $messageSend;
close $remote;
debug "ok: print \$remote: $messageSend";
while ( ($remoteAddress) = $local -> recv($receiveMessage,$bufferLength) )
{
debug 'ok: in recv_while $local -> recv()';
if($debug)
{
sleep(1);
}
($remotePort,$remoteIpNet)=sockaddr_in($remoteAddress);
delWithConnection();
last;
}
close $local;
debug 'ok: close $remote';
exit;
use IO::Socket;
$localPort = 7;
$bufferLength = 1024;
$timeArrayCount = 0;
@timeArray;
$timeIndex = 1;
$arrayMax = 3;
$debug = 1;
sub say
{
my $i;
foreach $i (@_)
{
print "$i\n";
}
}
sub debug
{
my $i;
foreach $i (@_)
{
print "$i\n";
}
}
sub timeStamp
{
my ($sec,$min, $hour, $mday, $mon,$year,$wday,$yday,$isdst);
($sec,$min, $hour, $mday, $mon,$year,$wday,$yday,$isdst)=localtime(time);
$year+=1900;
$mon+=1;
return "$year $mon $mday $hour:$min:$sec ";
}
sub delWithConnection
{
debug 'ok: inter while, delWithConnection';
$receiveMessageChomp = chomp($receiveMessage);
say "ok: received: $receiveMessage";
$remote = IO::Socket::INET ->
new(PeerPort => $remotePort,
PeerAddr => $remoteIp,
Proto => 'udp',
Reuse => 1,
LocalPort => $localPort
)
or die ("ERR: remote Socket new");
$remote -> autoflush();
if($debug)
{
}
print $remote "$receiveMessage\n";
debug "ok: print \$remote: $receiveMessage";
}
$local = IO::Socket::INET ->
new (LocalPort => $localPort,
Proto => 'udp',
Reuse => 1,
)
or die ("ERR: local Socket new");
$local -> autoflush();
debug "ok: IO::Socket::INET -> new";
while ( ($remoteAddress) = $local -> recv($receiveMessage,$bufferLength) )
{
$timeArray[$timeIndex] = timeStamp();
$timeIndex = $timeIndex + 1;
debug 'ok: in recv_while $local -> recv()';
($remotePort,$remoteIpNet)=sockaddr_in($remoteAddress);
$remoteIp = inet_ntoa($remoteIpNet);
say "ok: got a connection from $remoteIp:$remotePort";
delWithConnection();
close ($remote);
debug 'ok: close ($remote)';
say '';
if( $timeIndex-1 >= $arrayMax )
{
say "ok: Totally $arrayMax requests:";
for(my $i = 1; $i <= $arrayMax; $i = $i +1)
{
say "$timeArray[$i]";
}
last;
}
}
close ($local);
debug 'ok: close ($local);';
exit;
<!DOCTYPE html>
<html lang="en" style="height: 100%; overflow-y: hidden;">
<meta charset="UTF-8">
Test
<body style="height: 100%;"onload="getURL()">
<div style="height: 100%;">
<iframe id="iframe" src="http://www.baidu.com" width="100%" height="100%" frameborder="no" scrolling="auto">