Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

UDP broadcast using IO::Socket under Linux

by Tardis (Pilgrim)
on Jan 28, 2003 at 02:40 UTC ( #230464=perlquestion: print w/replies, xml ) Need Help??
Tardis has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am writing a small Perl/TK app, which communicates with other instances of itself on a LAN (think a sort of Instant Messaging application).

This snippet of code works under FreeBSD, but not under Linux:

#!/usr/bin/perl -w use IO::Socket; use strict; my($sock, $server_host, $msg, $port, $ipaddr, $hishost, $MAXLEN, $PORTNO, $TIMEOUT); $MAXLEN = 1024; $PORTNO = 5151; $server_host = '192.168.0.255'; $msg = "test"; $sock = IO::Socket::INET->new(Proto => 'udp', PeerPort => $PORTNO, PeerAddr => $server_host) or die "Creating socket: $!\n"; $sock->sockopt(SO_BROADCAST, 1); # $sock->sockopt(SO_DONTROUTE, 1); $sock->send($msg) or die "send: $!";
Running under FreeBSD works (as verified with a tcpdump process), running under Linux gives a:

Creating socket: Permission denied
I was given the two options to set on the socket (via 'sockopt') by a C programmer - the 'SO_DONTROUTE' doesn't seem to be necessary. Turning it on does not make it work under Linux. The 'SO_BROADCAST' is 100% necessary for it to work at all under FreeBSD.

Yes, I really want to use UDP broadcasts. Suggestions I use unicast addresses will be cheerfully ignored :-)

Oh, and although it would be totally unsuitable for my application, I did try running it as root. It made no difference.

Replies are listed 'Best First'.
Re: UDP broadcast using IO::Socket under Linux
by fokat (Deacon) on Jan 28, 2003 at 05:33 UTC

    Are you sure that 192.168.0.255 is your broadcast address? Under some OSes, you need to send your packet to 255.255.255.255 in order to send a broadcast packet. An easy way to test this is:

    bash# ping 192.168.0.255 ... bash# arp -a | egrep 192.168.0.255 ? (192.168.0.255) ff:ff:ff:ff:ff:ff

    You can easily see which broadcast address to use with:

    bash$ ifconfig eth0 | egrep -i broadcast

    However, I think there's no absolutely portable way to get to this information... All this said, you should really look into using multicast packets. Broadcasts have proven not scalable many times.

    Best regards

    -lem, but some call me fokat

Re: UDP broadcast using IO::Socket under Linux
by Tardis (Pilgrim) on Jan 30, 2003 at 05:22 UTC
    Update - I've since (while searching for my own node) found this one which seems to answer the question - though I have not tried it yet.

    If that node is correct, opening the socket via the OO interface does not work (for broadcasts), yet using the function call syntax does.

    When I get hold of a Linux box again, I'll try it :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://230464]
Approved by Courage
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-06-22 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (120 votes). Check out past polls.

    Notices?