[tpm] UDP broadcast/receive problems

James E Keenan jkeenan at pobox.com
Fri Oct 9 18:05:49 PDT 2020


On 10/9/20 8:41 PM, Fulko Hew wrote:
> I'm trying to accomplish what I thought was easy, and yet I can't get it 
> to work.
> 
> I have a number of devices that listen on UDP port 9999.
> So I want to send a broadcast message to my network and see all
> of their specific responses, so I can collect the list of IP addresses
> (for subsequent messaging).
> For a single device, it looks like this:
> 
> 192.168.1.149:port  --> 255.255.255.255:9999 <http://255.255.255.255:9999>
> 192.168.1.149:port  <-- 192.168.1.130:9999 <http://192.168.1.130:9999>
> 
> So I've tried a number of things starting from a single socket all the 
> way to
> a send socket and a second receive socket, but I can never read the 
> response that's sent.
> 
> My last attempt is this code snippet... can anyone tell me what's wrong ?
> 
> 
> $out = IO::Socket::INET->new(
>      PeerPort  => 9999,
>      PeerAddr  => inet_ntoa(INADDR_BROADCAST),
>      Proto     => udp,
>      ReuseAddr => 1,
>      Broadcast => 1)
>          or die "Can't bind : $@\n";
> 
> my $lport = $out->sockport();           # get the local port that was 
> assigned
> print "sending from $lport\n";
> 
> $in = IO::Socket::INET->new(
>      PeerPort  => 9999,
>      LocalPort => $lport,
>      Proto     => udp,
>      ReuseAddr => 1)
>          or die "Can't bind : $@\n";
> 
> my $s = IO::Select->new($in);
> $s->add($out);
> 
> $out->send('hello') or die "send: $!";
> 
> while ($i++ < 100) {
>      my @ready = $s->can_read(1);
>      foreach (@ready) {
>          die("readable but nothing read\n") unless 
> defined($_->recv($rsp, 1024));
>          print $rsp;
>      }
> }
> 

First suggestion:  use strict;

In particular, I can't tell what you mean by $rsp


More information about the toronto-pm mailing list