DatagramSocket Broadcast Behavior (Windows vs. Linux)

Background:
I have a wireless device that creates its own SSID, assigns itself an IP address using auto-ip, and starts broadcasting discovery information at 255.255.255.255. (unfortunately it doesn't support multicast)

What I am trying to do:
I need to get the discovery information and then send the configuration information to the device. The problem is that when using auto-ip, the "IP negotiation" process can take minutes on Windows, etc. (During which time I can see the broadcasts and even send the broadcast information back to the device).

So, I list all connected network interfaces (can't directly tell who will talk to the device), create a DatagramSocket for each of your addresses, and then start listening. If I receive discovery information over a specific socket, I know I can use the same socket to send data back to the device. This works on Windows.

Problem:
On Linux and OSX, the following code does not receive broadcast packets:

byte[] addr = {(byte)169, (byte)254, (byte)6, (byte)215};  
DatagramSocket foo = new DatagramSocket(new InetSocketAddress(InetAddress.getByAddress(addr), PORT_NUM));  
while (true)  
{  
byte[] buf = new byte[256];  
DatagramPacket pct = new DatagramPacket(buf, buf.length);  
foo.receive(pct);  
System.out.println( IoBuffer.wrap(buf).getHexDump() );  
}

      

To receive broadcast packets (on Linux / OSX) I need to create my DatagramSocket using:
DatagramSocket foo = new DatagramSocket(PORT_NUM);

However, when I then use that socket to send data back to the device, the packet is routed by the OS (I assume) and since the interface of interest might be in the middle of auto-ip negotiation fails.

Thoughts on the following?

  • How to make Windows behavior "work" on Linux / OSX
  • The best way to handle this process.

Thanks in advance!

+1


source to share


1 answer


I don't think this is a code issue. Have you checked if OSX / Linux has correctly allowed this address / port number through their firewalls? I had this simple problem and in the past = P ..

FYI, there is a good technology called Zero-configuration that was created to solve this problem. It's very easy to find out, so I recommend you take a look at this as well.



Good luck.

0


source







All Articles