From my understanding, UDP is for simple messaging and they are not gauranteed to make it thru, like TCP is. So if your UDP server is hit with
1000's of messages at the same time, you might want to switch to TCP(with IO::Select) OR have multiple servers listening to lighten the possible simultaneous load.
Anyways, this is from the Cookbook Chapter 17
#server
#!/usr/bin/perl -w
# udpqotd - UDP message server
use strict;
use IO::Socket;
my($sock, $newmsg, $hishost, $MAXLEN, $PORTNO);
$MAXLEN = 1024;
$PORTNO = 5151;
$sock = IO::Socket::INET->new(
LocalPort => $PORTNO,
Proto => 'udp') or die "socket: $@";
print "Awaiting UDP messages on port $PORTNO\n";
while ($sock->recv($newmsg, $MAXLEN)) {
my($port, $ipaddr) = sockaddr_in($sock->peername);
$hishost = gethostbyaddr($ipaddr, AF_INET);
print "Client $hishost said $newmsg\n";
$sock->send("CONFIRMED: $newmsg ");
}
die "recv: $!";
# You can't use the telnet program to talk to this server. You
# have to use a dedicated client.
#client
#!/usr/bin/perl -w
# udpmsg - send a message to the udpquotd server
use IO::Socket;
use strict;
my($sock, $msg, $port, $ipaddr, $hishost,
$MAXLEN, $PORTNO, $TIMEOUT);
$MAXLEN = 1024;
$PORTNO = 5151;
$TIMEOUT = 5;
$sock = IO::Socket::INET->new(Proto => 'udp',
PeerPort => $PORTNO,
PeerAddr => 'localhost')
or die "Creating socket: $!\n";
$msg = 'testmessage'.time;
$sock->send($msg) or die "send: $!";
eval {
local $SIG{ALRM} = sub { die "alarm time out" };
alarm $TIMEOUT;
$sock->recv($msg, $MAXLEN) or die "recv: $!";
alarm 0;
1; # return value from eval on normalcy
} or die "recv from localhost timed out after $TIMEOUT seconds.\n";
print "Server $hishost responded $msg\n";
# This time when we create the socket, we supply a peer host and
# port at the start, allowing us to omit that information in the send.
# We've added an alarm timeout in case the server isn't responsive,
# or maybe not even alive. Because recv is a blocking system call that
+
# may not return, we wrap it in the standard eval block construct
# for timing out a blocking operation.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.