in reply to How to answer a UDP broadcast

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.