I'm stuck with some code which creates a UDP datagram socket and broadcasts a message, a server process picks up the broadcast and attempts to send a reply but the client which sent the broadcast does not see it. Any ideas?
The client is something like this:
my $socket = IO::Socket::INET->new(
Broadcast => 1, Blocking => 1, ReuseAddr => 1,
Type => SOCK_DGRAM, Proto => 'udp', PeerPort => 9999,
LocalPort => 0,
PeerAddr => inet_ntoa(INADDR_BROADCAST)) ||
confess "error: failed to create broadcast udp socket - $!";
$socket->send('Q', 0);
my ($srcaddr, $data);
$srcaddr = $socket->recv($data, 100, 0);
die "recv: $!" if !defined($srcaddr);
close $socket;
and the server is like this:
my $socket = IO::Socket::INET->new(
Proto => 'udp', Type => SOCK_DGRAM,
LocalPort => 9999) ||
confess "error: failed to create broadcast udp socket - $!";
# server now does select etc to wait for msg
my $ip = $socket->recv(my $data, 100);
my ($port, $ipaddr) = sockaddr_in($socket->peername);
my $hishost = gethostbyaddr($ipaddr, AF_INET);
print "Client $hishost/$port said $data\n";
print "sent " . $scoket->send('Refresh', 0, $ip) . "\n";
and my server outputs:
Client xxx.yyy.local/47917 said Q
but the client just hangs and receives nothing.
This is Linux if it makes any difference and perl 5.8.8.