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

How I do broadcast in Perl with sockets?

I have the following code:

sub sendbroadcast { my $msg0 = $_[0] || ''; my $port = $_[1] || ''; my ($iaddr, $proto, $thataddr, $sockaddr, $msg); return 'Error. Port empyt' unless ($port); #Conf Conn # resolve protocol, port number $remhost= '192.168.0.255'; $iaddr= inet_aton($remhost) or $msg= "No host in sendbroadcast: $! - + $GREMOTE"; return "$msg - $remhost:$port" if ($msg); $sockaddr=sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp') || 6; socket(BRFD, PF_INET, SOCK_STREAM, $proto) or $msg= "Socket in sendbr +oadcast: $!"; return "$msg - $remhost:$port" if ($msg); connect(BRFD, $sockaddr) or $msg= "connect in sendbroadcast: $!"; return "$msg - $remhost:$port" if ($msg); $msg = $msg0 . '#' x (1024 - length($msg0)); syswrite BRFD, $msg, 1024; close (BRFD); return "Sent message"; }
but when executing it prints it: the network is inaccessible

Replies are listed 'Best First'.
Re: broadcast in Perl with sockets
by sgifford (Prior) on Mar 16, 2007 at 03:55 UTC
    It appears you are trying to broadcast TCP connections. TCP is connection-oriented, and doesn't support broadcast. Try using UDP instead.
Re: broadcast in Perl with sockets
by jesuashok (Curate) on Mar 16, 2007 at 01:32 UTC
    refer_samples and get to know what is the flaw in your code


    hmmm ....let me think what did I said