[tpm] UDP broadcast/receive problems

Fulko Hew fulko.hew at gmail.com
Fri Oct 9 18:42:19 PDT 2020


On Fri, Oct 9, 2020 at 9:06 PM James E Keenan <jkeenan at pobox.com> wrote:

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

OK, I added strict.  It didn't tell me anything I didn't already know.

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

$rsp is the receive buffer that'll contain each response to my broadcast.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20201009/deb8e169/attachment-0001.html>


More information about the toronto-pm mailing list