I am on Windows and using Strawberry Perl, and I also have a Windows virtual machine (VM) installed on my machine. The VM creates a network interface with the description "VirtualBox Host-Only Ethernet Adapter", and its IPv4 is: 192.168.56.1 (I configured the VM's network to run in bridge adapter mode, so the VM has an Ethernet interface at address 192.168.1.12). Normally, I use a wireless interface with an IPv4 address at 192.168.1.6.

I want to broadcast in the local subnet. So I ran this code:

use strict; use warnings; use IO::Socket::INET; my $broadcast_address = '255.255.255.255'; my $socket = IO::Socket::INET->new( Proto => 'udp', Broadcast => 1, LocalPort => 8888, PeerAddr => $broadcast_address, PeerPort => 9999, ) or die "Could not create socket: $!\n"; my $message = "Broadcast message!"; $socket->send($message) or die "Send error: $IO::Socket::errstr\n"; my $hostAddr = $socket->sockhost(); my $hostPort = $socket->sockport(); print "Broadcast message sent from address $hostAddr:$hostPort\n"; $socket->close();

The problem is it printed this out: "Broadcast message sent from address 192.168.56.1:8888".. The receiver couldn't receive the broadcast message from 192.168.56.1 address, but if I changed the $broadcast_address to 192.168.1.255, it would work.

I diagnosed that the Perl module automatically chooses 192.168.56.1 as the main IP address and then uses it to send messages. How do I get rid of that? And how to make the wireless interface the chosen?

Thanks in advance.


In reply to Broadcast when host machine contains virtual interface by Anonymous Monk

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.