worley@compass.com (Dale Worley) (08/06/90)
Is there any way to communicate with a server that use UDP (rather than TCP) using Perl? Thanks, Dale Worley Compass, Inc. worley@compass.com -- Machines will never think, for "thought" will be redefined, as often as needed, as that which a machine cannot do.
lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (08/07/90)
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 (<STDIN>) { print S; } sleep 2; do dokill(); } else { while (read(S, $_, 32767)) { print; print "Remote $host# "; } } sub dokill { kill 9,$child if $child; }