perl实现UDP通信

UDP Client and Server

# client
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',		#ringht
		Reuse => 1,
#		Listen => 5,
		)
	or die ("ERR: local Socket new");
$local -> autoflush();

$remote = IO::Socket::INET -> 
	new(PeerPort	=>	$remotePort,
		PeerAddr	=>	$remoteIp,
#		Type		=>	SOCK_DGRAM,
		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) = $remote -> recv($receiveMessage,$bufferLength) )
#while ( ($remoteAddress) = $remote -> recv($receiveMessage,$bufferLength) )
while ( ($remoteAddress) = $local -> recv($receiveMessage,$bufferLength) )
{
	debug 'ok: in recv_while $local -> recv()';
	
	if($debug)
	{
		sleep(1);
	}
	($remotePort,$remoteIpNet)=sockaddr_in($remoteAddress);
#	$remoteIp = inet_ntoa($remoteIpNet);
#	say "ok: got the echo from $remoteIp:$remotePort";

	delWithConnection();

	last;
}

close $local;

debug 'ok: close $remote';
exit;


# server
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)
	{
		#sleep (1);
	}
	print $remote "$receiveMessage\n";
	debug "ok: print \$remote: $receiveMessage";
}

$local = IO::Socket::INET -> 
	new	(LocalPort => $localPort,
		Proto => 'udp',		#ringht
		Reuse => 1,
#		Listen => 5,
#		Listen => 5,
		)
	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">
<script type="text/javascript"> function getURL() { var str = location.href; //取得整个地址栏 var arr = str.split("?"); //各个参数放到数组里 if (arr.length > 1){ document.getElementById("iframe").src=arr[1]; } }

你可能感兴趣的:(udp通信,perl)