#!/opt/net/GNU/bin/perl -w # # receives numbered UDP datagrams from a remote host. # # Usage: # udp-recv [-p ] # # defaults to 9870. # # Renaud Waldura # Mon Sep 18 17:30:18 1995 # require 'getopts.pl'; ## parse options &Getopts('p:'); $port = ($opt_p) ? $opt_p : 9870; ## create a UDP socket $sockaddr = 'S n a4 x8'; # magic $family = 2; # AF_INET (system dependent !) $socktype = 1; # SOCK_DGRAM (system dependent !) ($name, $aliases, $proto) = getprotobyname('udp'); $me = pack($sockaddr, $family, $port, "\0\0\0\0"); # make the socket and bind it to the protocol print "name $name aliases $aliases proto $proto\n"; socket(S, $family, $socktype, $proto) || die "Unable to create socket: $!"; bind(S, $me) || die "Unable to bind socket: $!"; ## receive numbered datagrams and print their values $sender = ""; $datagram = ""; for ($n = 1; ; $n++) { $sender = recv(S, $datagram, 1024, 0); print "Received datagram $n, length = ", length($datagram), ", number = ", (split(/:/, $datagram))[1], "\n"; # ($af, $port, $inetaddr) = unpack($sockaddr, $sender); # @inetaddr = unpack('C4', $inetaddr); # print "\tfrom { $af, $port, @inetaddr }\n"; } ## die close(S);