Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#42 listen to UDP broadcasts fails after one

open
nobody
5
2008-01-04
2008-01-04
Anonymous
No

try this:
nc -u -l -p 8888

then send a couple
echo hi |nc -u -b hostname 8888

you will receive the first one but none after that (unless they are unicast) It seems nc switches to unicast mode after receiving the first one. Might be nice, but I want to continuously monitor broadcasts.
Suggestion: nc -u -b -l -p 8888

the -b in listen mode could mean never switch to unicast.

--Dave
clist44@farsiteonline.net

Discussion

  • Logged In: NO

    P.S. version 1.10
    --Dave

     
  • Logged In: NO

    P.P.S. hostname in the "sending" example means broadcast address... sorry. Need more coffee.

     
  • Dave
    Dave
    2008-01-22

    Logged In: YES
    user_id=1410238
    Originator: NO

    Here's a "nailfile" to use while waiting for the swiss army knife to learn the trick ;)
    This will listen to udp broadcasts (and/or unicasts) all day and all night... you can
    pretty much see how to tinker around with it for your needs.
    Cheers,
    Tinker45

    <code>
    #!/usr/bin/perl
    #
    # UDP Listen
    #
    use IO::Socket::INET;
    use strict;

    my ($sock);
    my $data;

    $sock=IO::Socket::INET->new(Proto => 'udp',
    LocalPort => 6789) or die "Can't bind: $@\n";

    print scalar localtime().": Awaiting data...\n";
    while($sock->recv($data, 1024)) {
    my ($port, $ipaddr) = sockaddr_in($sock->peername);
    my ($peerhost)=gethostbyaddr($ipaddr, AF_INET);
    my ($peerip) = inet_ntoa($ipaddr);

    print scalar localtime().": Rcvd $data from $peerhost $peerip\n";
    }

    print "Done\n";

    </code>

     
  • I can confirm this bug. I see the same results.

    --Val

     
  • Ralph Corderoy
    Ralph Corderoy
    2011-07-18

    When listening with UDP, nc uses the first packet received to connect(2) to the sender's host and port. From then on, only packets from that same host and port are received.