Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thank you very much

I followed your advice, but somehow the send doesn't reach the server (on the same host). The network is fine, and Ikegamis script works perfect (except that it is not broadcasting).

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_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

#! /usr/bin/perl 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, "a" x 4096, 0, $broadcastAddr ) or die "Error at sendding: $!\n"; close SOCKET;

Does anybody know what I'm doing wrong?

My friend intends to use broadcasts to announce a new server going online to a central service. He won't misuse it. I am interested in this topic, because I know a little bit about TCP/IP, but have hardly any knowledge about UDP and broadcasts.

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


In reply to Re^2: UDP-Broadcast with socket by strat
in thread UDP-Broadcast with socket by strat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found