Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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:


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 = ''; $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 is your broadcast address? Under some OSes, you need to send your packet to in order to send a broadcast packet. An easy way to test this is:

    bash# ping ... bash# arp -a | egrep ? ( 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://230464]
Approved by Courage
[Lotus1]: I was just pleasantly surprised to find I have shasum on my Windows machine. It was included in Strawberry Perl

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-03-18 00:09 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (228 votes). Check out past polls.