#!/usr/bin/perl -w # udpmsg - send a message to the udpquotd server use IO::Socket; use strict; my($sock, $msg, $port, $ipaddr, $hishost, $MAXLEN, $PORTNO, $TIMEOUT); $MAXLEN = 1024; $PORTNO = 5151; $TIMEOUT = 5; $sock = IO::Socket::INET->new(Proto => 'udp', PeerPort => $PORTNO, PeerAddr => 'localhost') or die "Creating socket: $!\n"; $msg = 'testmessage'.time; $sock->send($msg) or die "send: $!"; eval { local $SIG{ALRM} = sub { die "alarm time out" }; alarm $TIMEOUT; $sock->recv($msg, $MAXLEN) or die "recv: $!"; alarm 0; 1; # return value from eval on normalcy } or die "recv from localhost timed out after $TIMEOUT seconds.\n"; print "Server $hishost responded $msg\n"; # This time when we create the socket, we supply a peer host and # port at the start, allowing us to omit that information in the send. # We've added an alarm timeout in case the server isn't responsive, # or maybe not even alive. Because recv is a blocking system call that # may not return, we wrap it in the standard eval block construct # for timing out a blocking operation.