Posted on 2010-08-02 17:52
kongkongzi 阅读(298)
评论(0) 编辑 收藏 引用 所属分类:
perl
Server program code :
#!/usr/bin/perl -w
use strict;
# Server Program
use IO::Socket::INET;
print ">> Server Program <<\n";
# Create a new socket
my $MySocket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp');
# Keep receiving messages from client
my $def_msg="\nReceiving message from client..\n";
while(1)
{
my $text;
$MySocket->recv($text,128);
if($text ne '')
{
print "\nReceived message '", $text,"'\n";
}
# If client message is empty exit
else
{
print "Cilent has exited!";
exit 1;
}
}
Client program code :
#!/usr/bin/perl -w
use strict;
# Client Program
use IO::Socket::INET;
print ">> Client Program <<";
# Create a new socket
my $MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',PeerAddr=>'localhost');
# Send messages
my $def_msg="Enter message to send to server : ";
print "\n",$def_msg;
my $msg;
while($msg=<STDIN>)
{
chomp $msg;
if($msg ne '')
{
print "\nSending message '",$msg,"'";
if($MySocket->send($msg))
{
print "..<done>","\n";
print $def_msg;
}
}
else
{
# Send an empty message to server and exit
$MySocket->send('');
exit 1;
}
}