http://www.perlmonks.org?node_id=735745

strat has asked for the wisdom of the Perl Monks concerning the following question:

Update: added client script, changed INADDR_BROADCAST to INADDR_ANY for server (thanks @ikegami)

Good day,

a friend of mine asked me how UDP-Broadcasts with Socket worked. I hardly know anything about UDP, but I tried to learn a little bit about and wrote the following two pieces of code:

server.pl
#! /usr/bin/perl use warnings; use strict; use Socket qw(:all); $|++; # no suffering from buffering my $udp_port = 9999; socket( UDPSOCK, PF_INET, SOCK_DGRAM, getprotobyname('udp') ) or die " +socket: $!"; select( ( select(UDPSOCK), $|=1 )[0] ); # no suffering from buffering setsockopt( UDPSOCK, SOL_SOCKET, SO_REUSEADDR, 1 ) or die "setsockopt SO_REUSEADDR: $!"; setsockopt( UDPSOCK, SOL_SOCKET, SO_BROADCAST, 1 ) or die "setsockopt SO_BROADCAST: $!"; my $broadcastAddr = sockaddr_in( $udp_port, INADDR_ANY ); bind( UDPSOCK, $broadcastAddr ) or die "bind failed: $!\n"; my $input; while( my $addr = recv( UDPSOCK, $input, 4096, 0 ) ) { print "$addr => $input\n"; }
Client.pl:
use warnings; use strict; use Socket qw(:all); use POSIX ":sys_wait_h"; socket( SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp") ) or die "Error: can't create an udp socket: $!\n"; select( ( select(SOCKET), $|=1 )[0] ); # no suffering from buffering my $broadcastAddr = sockaddr_in( 9999, INADDR_BROADCAST ); setsockopt( SOCKET, SOL_SOCKET, SO_BROADCAST, 1 ); send( SOCKET, "I'm here", 0, $broadcastAddr ) or die "Error at sendding: $!\n"; close SOCKET;

But the server waits forever, it doesn't get any message from client.pl. I started the scripts on the same PC, an OpenSuse 10.3 Linux with perl5.8.

Please, can you give me a hint what I'm doing wrong? I searched perlmonks and google, but couldn't find anything helpful.

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"