fjonckers has asked for the wisdom of the Perl Monks concerning the following question:

strange... the following script doesn't work under Linux
I get following error:
send: Cannot determine peer address at udp.pl line 24
in Windows it works fine (activeperl)
anyone has an idea why ?????
#!/usr/bin/perl # use strict; use IO::Socket; # defaults my $bcaddr = '192.168.254.255'; my $port = 9870; # open UDP socket my $sock = IO::Socket::INET->new( Proto=>'udp', LocalPort=>$port, PeerHost=>$bcaddr, PeerPort=>$port) or die "new socket:$@"; # 10 udp's on the wire for (my $i=0;$i < 10;$i++) { my $data = 'UDP packet ' . $i; # send udp packet $sock->send($data) || die "send(): $!"; }

Replies are listed 'Best First'.
Re: udp broadcast: doesn't work in Linux
by fjonckers (Novice) on Oct 12, 2001 at 01:27 UTC
    ok guys, found it...
    The option SO_BROADCAST requests permission to send broadcast datagrams on the socket. Broadcast was a privileged operation in earlier versions of the system.
    so, I rewrote my snippet:
    #!/usr/bin/perl # use strict; use IO::Socket; # defaults my $bcaddr = '192.168.254.255'; my $port = 9870; socket(sock, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket:$@"; setsockopt(sock, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt:$@"; my $dest = sockaddr_in($port,inet_aton($bcaddr)); # 10 udp's on the wire for (my $i=0;$i < 10;$i++) { my $data = 'UDP packet ' . $i; # send udp packet send(sock,$data,0,$dest) || die "send(): $!"; }
    doesn't seem to work with IO::Socket::INET->new
    thanks for the tips! made me look further than Perl (C, kernel)...
Re: udp broadcast: doesn't work in Linux
by cjensen (Sexton) on Oct 11, 2001 at 21:39 UTC
    Well, this is tough, because it seems your script isn't at fault.

    It looks like the call to getpeername($sock) is failing for this socket in IO::Socket.

    Try calling that directly in your script and see if you get the error at that point.

    Try connecting to another host/port. It's hard to diagnose without knowing more about your network config and the circumstances of this test.

      thanks cjensen!
      I added the following just below the socket creation and above the send method:
      print "\ngetpeername: " . getpeername($sock) . "\n";
      output is:
      getpeername: send: Cannot determine peer address at udp.pl line 26
      so, no error when calling getpeername...
      checking the linux kernel road.. woulb be very strange if you cannot send something to a LAN broadcast address
      tried other port values... nothing (port is random)
      F.
        I should have been more clear... The error is occurring when the peer name is undefined in IO::Socket. It won't give you an error when it can't determine the peer address, it just returns undef. Then the call to send in IO::Socket croaks when peer name is undefined:

            croak 'send: Cannot determine peer address'
                 unless($peer);
        
        So getpeername($sock) returning undef is what I expected would happen, I just didn't explain that well enough. It should return the address of the peer at the remote end of the socket.

        Try this and vary the $bcaddr in your script:

        my ($port, $addr) = unpack_sockaddr_in(getpeername($sock));
        my $host = gethostbyaddr($addr, AF_INET);
        print "Host:\t$host\n";
        print "Port:\t$port\n";


Re: udp broadcast: doesn't work in Linux
by perrin (Chancellor) on Oct 11, 2001 at 21:17 UTC
    I think it requires support compiled into the kernel. At least it used to. Check on the Linux newsgroups or help sites for configuration info.
Re: udp broadcast: doesn't work in Linux
by Anonymous Monk on Oct 11, 2001 at 23:07 UTC
    yes make sure your udp stuff
    is set up properly in the kernel

    http://www.linuxdocs.org/HOWTOs/Multicast-HOWTO.html
      thanks for your reply Ano!
      but I'm using an UDP BROADCAST not a Multicast...
      I want to send a packet to the broadcast address of my local subnet
      network: 192.168.254.0/24 where the broadcast address is 192.168.254.255
      Multicasting is something completely different, broadcast addresses is standard ip stuff...
      Filip