DatagramSocket bindtoPort() not binding to right Interface


#1

    Array<IPAddress> ips;
    IPAddress::findAllAddresses(ips);
    socket = new DatagramSocket(true);
    Logger::outputDebugString(ips[1].toString());
    socket->bindToPort(0,ips[1].toString()); //let OS choose port
    socket->write("255.255.255.255", 50000, buffer, 54);

I have two interfaces on my computer and I would like to pick the right one to broadcast this message. I thought bindtoPort() with a localadress option is to select the interface. Used Logger to see if it's the right IP passed to the function and it is. The function is returning true, but it still broadcasts on the wrong interface? Any help ?

 

 


#2

No idea how it works with multiple interfaces! Presumably you have to give them different subnet masks or something... Doubt whether this is anything we'd need to change within our classes, as it's presumably just a mattter of using the right IP address or port, but my networking chops aren't good enough to know what to suggest.


#3

On the documentation it's stating that this function could be used to select the right interface. Even though the function returns with success it doesn't select the right interface.

I have two interfaces on my MBP. Wifi and ethernet. 

WIFI is connected to the internet and has the IP 192.168.1.33, and ethernet is connected to the local network with an IP address of 169.254.214.12. 

When I get the IPs of all interfaces it returns the following results;

ips[0] = 127.0.0.1 lo

ips[1] = 169.254.214.12

ips[2] = 192.168.1.33

But eventhough I use the bindToPort function with the ethernet's IP address, it still sends the data to Wifi's network(192.168.1.33). 

 


#4

Haven't a clue, TBH.

But I don't think this is a JUCE problem, it's more a general sockets question. All that our classes do is to pass the IP address along to the normal posix functions. Hopefully someone else reading this might know enough about it to help you..


#5

I don't know why you have done such thing, might be a good reason behind it but the code below in juce_Socket.cpp basically ignores the adress passed to bindtoPort on any OS other than Windows. I've just commented it out, and it works at the moment. I'm neither a C++ expert or a network expert either, I would like to know if that would cause any issues in general. 

   //#if JUCE_WINDOWS

        if (address.isNotEmpty())

            servTmpAddr.sin_addr.s_addr = ::inet_addr (address.toUTF8());

   //  #else

       //ignoreUnused (address);

   //  #endif

#6

Seems to be this commit here. I'm not exactly sure why this was needed. Is inet_addr not available on all platforms?


#7

FYI I've removed that restriction now - inet_addr was used elsewhere in the code so must be available everywhere. If anyone hits an issue, let us know!