#!/usr/bin/perl -w
# udpqotd - UDP message server
use strict;
use IO::Socket;

my($s, $i, $sock, $newmsg, $hishost, $ipport, $port, $ipadr, $adr, 
$MAXLEN);
our(@ARGV);

( $adr, $port ) = ( 0, 54321 );
$MAXLEN = 1024;

if ( $ARGV[0] ){ $adr = $ARGV[0]; print "listening on $adr $port\n" }

$sock = IO::Socket::INET->new(
        LocalPort => $port,
#        LocalAddr => '129.16.255.255',
        Proto     => 'udp',
        Reuse     => 1) or die "socket: $@";

print "Awaiting UDP messages on port $port\n";

while ($sock->recv($newmsg, $MAXLEN)) {
    ($port, $adr) = sockaddr_in($sock->peername);
    $hishost = gethostbyaddr($adr, AF_INET);
    print "$hishost :: $newmsg";
    $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. 
exit
