[tpm] UDP broadcast/receive problems

Fulko Hew fulko.hew at gmail.com
Fri Oct 9 20:18:26 PDT 2020


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

> On 10/9/20 9:42 PM, Fulko Hew wrote:
> >
> >
> > On Fri, Oct 9, 2020 at 9:06 PM James E Keenan <jkeenan at pobox.com
> > <mailto: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> <http://255.255.255.255:9999>
> >      > 192.168.1.149:port  <-- 192.168.1.130:9999
> >     <http://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) {
>
> In the line above, are you trying to say, "do this 100 times"?
>

Yes, although the foreach loop below allows me
to scan and process all of the messages on each socket
that 'currently' have messages to read... the while loop allows me to
look for stuff a number of times, in case messages come in later.
(the 1 in the can_read() signifies a one second timeout, so the
100 loops in the while() allows me to listen and process responses
that can arrive up to 100 seconds later.)  Don't obsess about the
timeouts, it's just mainly for debugging purposes.

Anyway, and my followup says, I now have to deal with the firewall config
issue.

> >      >      my @ready = $s->can_read(1);
> >      >      foreach (@ready) {
> >      >          die("readable but nothing read\n") unless
> >      > defined($_->recv($rsp, 1024));
>
> What is $_ in the line above?
>

The next socket, from the list of sockets that have stuff awaiting to be
read.


> >      >          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/cfb87fb5/attachment-0001.html>


More information about the toronto-pm mailing list