Note that in this thread the poster seems to have a problem related to mine: DatagramSocket Broadcast Behavior (Windows vs. Linux) Unfortunately this post died without any solution to the problem.
I am currently having some problems with Datagramsockets in Java. Overall the problem is that I have a piece of Java code that is able to receive packets on Windows but not linux.
It is critical that my application
- Is able to work on both Windows and linux. It is okay to say that we only support Windows 7 and later.
- Works on machines with multiple network interfaces. By this I mean that it should be possible to specify which network interface the application should receive its packets from.
My test machines
- m1 : Running windows 7 Pro x64 on a physical machine. java version "1.8.0_45" (Oracle)
- m2: Running centos7 x64 in vmware. Using java version "1.8.0_51" (Oracle)
- m3: Runnning fedora 19 x64 on a physical machine. Using java version 1.7.0_71 (openjdk)
- m4: Runnning fedora 19 x64 in vmware. java version "1.8.0_31"
- m5: Running windowsXP x86 on a physical machine : This machine is not running my program but instead an external system broadcasting the packets I want to receive.
- m6: Running Windows 7 Pro x64 on a physical machine: This machine is not running my program but also an external system broadcasting the packets I want to receive.
Currently my program receives the packets from m5 and m6 on m1 but not m2, m3 and m4.
Overall the logic behind the sockets in my program can be described as follows
// Setting up the socket
DatagramSocket socket = new DatagramSocket(networkInterfaceAddress, port);
socket.setReceiveBufferSize(90000);
// Setting up the receive packets
DatagramPacket packet = new DatagramPacket(new byte[1], 0);
// Constructing the receive buffer
buffer = new byte[1024]
// Receiving the packet
packet.setData(buffer);
packet.setLength(buffer.length);
socket.receive(packet);
I have tried the following:
- Disabled the firewall on all machines. This didn't help
- Used Wireshark on m2, m3 and m4. Wireshark was able to detect the packets on all machines, so they do indeed receive them ( 4. also indicates this)
- Changed my test setup s.t. I broadcasted from m2 instead (using my own application, not the external system) Neither m1, m3 or m4 was able to receive the packets. The funny thing is that I was able to see them in the external systems on m5 and m6.
- Rewrote the logic s.t. I didn't specify a networkInterfaceAddress in the constructor for the DatagramSocket (So I used socket = new DatagramSocket(port);. With this update I was suddenly able to receive the packets on m2, m3 and m4
4 seems to indicate that the problem is the networkInterfaceAddress but if I try running the program in debug mode I can see that networkInterfaceAddress does contain the expected value. To be sure I have also tried a hardcodet dummy example on m2. In this Dummy example I
- Used ifconfig to see the address of my network interface. In my case ifconfig gave me init 10.10.1.41
- Used networkInterfaceAddress = InetAddress.getByName("10.10.1.41");
Even with this hardcodet networkinterface I wasn't able to receive the packets.
So currently I am only able to receive packets on linux by not supplying a network interface to the DatagramSocket constructor. The problem with this solution is that (as far as I understand) this will cause the application to be unable to only receive packets from a specific network interface.
To be honest I am running out of ideas so I am hoping that someone might be able to help me solve this problem.