Somehow I did not believe you cannot use a UDP socket to broadcast and receive responses. When I looked I had C code which does precisely this.
The problem appears to be my use of IO::Socket or IO::Socket as if the client code above is replaced with the following it all works fine. The PeerAddr causes IO::Socket to do a connect so the only way I can make IO::Socket do it was to remove PeerAddr (and hence PeerPort) and change the send to use INADDR_BROADCAST.
socket(my $socket, AF_INET, SOCK_DGRAM, getprotobyname('udp'));
setsockopt($socket, SOL_SOCKET, SO_BROADCAST, 1);
my $destpaddr = sockaddr_in(9990, INADDR_BROADCAST);
send($socket, 'Q', 0, $destpaddr);
my $wait = IO::Select->new($socket);
while (my ($found) = $wait->can_read(10)) {
my $srcpaddr = recv($socket, my $data, 100, 0);
my ($port, $ipaddr) = sockaddr_in($srcpaddr);
print "Read " . (gethostbyaddr($ipaddr, AF_INET) || "UNKNOWN") . ",
+ " . inet_ntoa($ipaddr) . ", ", $data, "\n";
}
close $socket;
i.e. you can open a udp socket, broadcast on it and then receive data on it sent from the processes seeing the broadcast.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.