Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!aplcen!uakari.primate.wisc.edu!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: UDP Message-ID: <9032@jpl-devvax.JPL.NASA.GOV> Date: 6 Aug 90 22:03:45 GMT References: <1990Aug6.144416.29167@uvaarpa.Virginia.EDU> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 65 In article <1990Aug6.144416.29167@uvaarpa.Virginia.EDU> worley@compass.com writes: : Is there any way to communicate with a server that use UDP (rather than : TCP) using Perl? Yes, but it's unreliable. :-) I do it regularly. Here's a client that will send to a UDP echo port. You can either use a connected socket or a send with the optional "to" argument. You should also be able to substitute a recv for the read. Larry #!/usr/bin/perl ($host) = @ARGV; die "usage: $0 hostname\n" unless $host; $pat = 'S n C4 x8'; $stream = 1; $datagram = 2; $inet = 2; $tcp = 6; $udp = 17; ($name,$aliases,$port) = getservbyname('echo','udp'); $SIG{'INT'} = 'dokill'; if ($host =~ /^\d+\./) { @bytes = split(/\./,$host); } else { ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host); die "Can't lookup $host\n" unless $name; @bytes = unpack("C4",$addrs[0]); } $this = pack($pat,$inet,0, 0,0,0,0); $that = pack($pat,$inet,$port,@bytes); socket(S,2,$datagram,$udp) || die "socket: $!\n"; bind(S,$this) || die "bind: $!\n"; connect(S,$that) || die "connect: $!\n"; select(S); $| = 1; select(stdout); $| = 1; if ($child = fork) { print "Remote $host# "; while () { print S; } sleep 2; do dokill(); } else { while (read(S, $_, 32767)) { print; print "Remote $host# "; } } sub dokill { kill 9,$child if $child; }