Thread Broadcastdaten vom Netz holen (IPv4)
(2 answers)
Opened by JoeMiller at 2011-05-11 10:13
Peer addr braucst du nicht => ist zum senden
Local addr muss die von deinem interface sein. broadcast msg kommen dann von selbst bei dir an. der IP stack reicht die hoch da sie ja broadcast sind. das folgende script tut. kannst du mit nc -b -u <IP> 2000 testen Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/usr/bin/perl -w # udpqotd - UDP message server use strict; use IO::Socket; my($sock, $oldmsg, $newmsg, $hisaddr, $hishost, $MAXLEN, $PORTNO); $MAXLEN = 1024; $PORTNO = 2000; $sock = IO::Socket::INET->new(LocalPort => $PORTNO, Proto => 'udp') or die "socket: $@"; print "Awaiting UDP messages on port $PORTNO\n"; $oldmsg = "This is the starting message."; 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($oldmsg); $oldmsg = "[$hishost] $newmsg"; } die "recv: $!" Last edited: 2011-05-11 12:46:58 +0200 (CEST) |