No, you don't have to connect to INADDR_BROADCAST.

use strict; use warnings; use IO::Socket::INET qw( INADDR_BROADCAST inet_ntoa pack_sockaddr_in u +npack_sockaddr_in ); use threads ;#qw( async ); my $client = async { my $s = IO::Socket::INET->new( Proto => 'udp', LocalPort => 7071, Broadcast => 1, ) or die("Can't create socket: $!\n"); defined( my $peer = $s->recv(my $msg, 64*1024, 0) ) or die("Can't recv: $!\n"); my ($port, $addr) = unpack_sockaddr_in($peer); $addr = inet_ntoa($addr); print("Received $msg from $addr:$port\n"); $s->send("Pong!", 0, $peer); }; { my $s = IO::Socket::INET->new( Proto => 'udp', Broadcast => 1, ) or die("Can't create socket: $!\n"); $s->send("Ping!", 0, pack_sockaddr_in(7071, INADDR_BROADCAST)); defined( my $peer = $s->recv(my $msg, 64*1024, 0) ) or die("Can't recv: $!\n"); my ($port, $addr) = unpack_sockaddr_in($peer); $addr = inet_ntoa($addr); print("Received $msg from $addr:$port\n"); } $client->join();
Received Ping! from 10.0.0.6:49853 Received Pong! from 10.0.0.6:7071

In reply to Re^5: Sending AND Receiving on the Same Port (udp broadcast example) by ikegami
in thread Sending AND Receiving on the Same Port by HalNineThousand

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.