#!/usr/bin/perl -w
# UDP message server
# Empfaengt eine Machricht vom Client und sendet die zuletzt
# empfangene Nachricht zurueck

use strict;
use IO::Socket;

my($sock, $oldmsg, $newmsg, $hisaddr, $hishost, $MAXLEN, $PORTNO);

$MAXLEN = 1024;
$PORTNO = 5151;

$sock = IO::Socket::INET->new(LocalPort => $PORTNO, Proto => 'udp')
    || die "socket: $@";
print "Erwarte UDP-Nachricht auf Port $PORTNO\n";
$oldmsg = "Start-Nachricht";

while ($sock->recv($newmsg, $MAXLEN)) 
  {
  my($port, $ipaddr) = sockaddr_in($sock->peername);
  $hishost = gethostbyaddr($ipaddr, AF_INET);
  print "Client $hishost sendete: $newmsg\n";
  $sock->send($oldmsg);
  $oldmsg = "[$hishost] $newmsg";
  } 
die "recv: $!";