- 论坛徽章:
- 0
|
如何编写在某端口上接收udp消息的脚本?
try this:
- #!/usr/bin/perl -w
- # UDP message server $id :udps.pl
- use strict;
- use IO::Socket;
- my($sock, $msg, $hisaddr, $hishost, $MAXLEN, $PORTNO);
- $MAXLEN = 1024;
- $PORTNO = 8088;
- $sock = IO::Socket::INET->;new(LocalPort =>; $PORTNO, Proto =>; 'udp')
- or die "socket: $@";
- print "Awaiting UDP messages on port $PORTNO...\n";
- while ($sock->;recv($msg, $MAXLEN)) {
- my($port, $ipaddr) = sockaddr_in($sock->;peername);
- $hishost = gethostbyaddr($ipaddr, AF_INET);
- print "Client $hishost said ``$msg''\n";
- $sock->;send("received!-- $msg --OK");
- }
- die "recv: $!";
复制代码
start cmd:
#udps.pl |
|