in reply to How Do I Do an IP Broadcast in Perl?

Try these, for simplicity. Start the server as root (UDP requires root privileges, IIRC) , and then run instances of the client.

udp server

#!/usr/bin/perl -w # udpqotd - UDP message server use strict; use IO::Socket; my($sock, $newmsg, $hishost, $MAXLEN, $PORTNO); $MAXLEN = 1024; $PORTNO = 5151; $sock = IO::Socket::INET->new( LocalPort => $PORTNO, Proto => 'udp') or die "socket: $@"; print "Awaiting UDP messages on port $PORTNO\n"; 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("CONFIRMED: $newmsg "); } die "recv: $!"; # You can't use the telnet program to talk to this server. You # have to use a dedicated client.

udp client

#!/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.

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: How Do I Do an IP Broadcast in Perl?
by tirwhan (Abbot) on Sep 27, 2010 at 12:33 UTC

    How does this relate to what the OP is asking? Your code still relies to the client knowing the servers IP address and won't help him solve his problem. Which (if you read the thread) he did anyway, almost 12 hours before you posted your node, so what's the point? Also:

    UDP requires root privileges, IIRC

    Err, no. Stick to port numbers <1024 >1024 and you can send/receive UDP just fine as an unprivileged user.

    Update: corrected port numbers, thanks Corion for the heads up.


    All dogma is stupid.
      (I know how to get the subnet and all else, just can't send the broadcast!)

      Just showing him a working example of sending the broadcast, and many systems are setup that non-root user access is disallowed for ports < 1024


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        Just showing him a working example of sending the broadcast,

        But you don't, you're sending to localhost.

        and many systems are setup that non-root user access is disallowed for ports < 1024
        It should be >1024 of course (as Corion rightly pointed out to me). But no "normal" system I know entirely restricts users from sending UDP packets over the network.

        All dogma is stupid.