1
votes

I'm encountering a strange problem when working with UDP in Java. Setup: My Android device is initializing the communication, it is connected via Wifi. My computer runs a server written in Java (JRE: 1.8) and is connected via Ethernet.

I'm sending an UDP packet from an Android device to my computer:

address = InetAddress.getByName("192.168.178.57");
packet = new DatagramPacket(data, data.length, address, 61010);
socket.send(packet);

There is no exception thrown while sending the packet. Having Wireshark opened on my computer, I can see the UDP packet being received:

Wireshark: UDP packet received

In my server application I'm trying to receive this UDP packet:

address = InetAddress.getByName("192.168.178.57");
socket = new DatagramSocket(61010, address);
byte[] receiveBuffer = new byte[4096];
System.out.println("listening on " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort() + "...");
// => listening on 192.168.178.57:61010...

packet = new DatagramPacket(receiveBuffer, receiveBuffer.length);
socket.receive(packet);
System.out.println("UDP message received");

However, it is not received: UDP message received is never being printed.

[edit]
The firewall should be configured to allow the packets (ufw allow 61010/udp). There is no security manager in Java that would drop the packet (System.getSecurityManager() is null). But if I send the UDP packet via the server application, instead of the Android device, it is received.
[/edit]

I am pretty sure that this code worked a couple of months ago and that I didn't work on it in between.

What am I missing, any ideas?

1
Check the firewall configuration on the server computer. It is plausible that the packets are being dropped after wireshark logs them. - John Bollinger
As far as I can tell, there is no rule that would lead to this behaviour. Now I've explicitly allowed UDP communication on this port via sudo ufw allow 61010/udp, with no effect. - Sebastian Schlicht
On the server side, it is often preferable to bind to a wildcard address, so that the server doesn't need to know or determine its own address. You can accomplish that by using the DatagramSocket constructor that takes only the port as an argument. - John Bollinger
Yeah, I had this before. I switched to the very exact IP address for debugging, in order to reduce the number of potential pitfalls. - Sebastian Schlicht
The docs of DatagramSocket.receive() note that "If there is a security manager, a packet cannot be received if the security manager's checkAccept method does not allow it." It is thus possible that your security policy interferes with receiving anything. This is also an area that could have changed. - John Bollinger

1 Answers

0
votes

Thanks to @John Bollinger I had a second and third look on the firewall setup. Indeed, the firewall was causing this issue.

Explicitly allowing the particular port via ufw allow <port>/udp apparently was not enough to bypass whatever rule that has been blocking external UDP packets.

After resetting the iptables rules the Android App now is able to communicate with my server application again.

Thanks for your input!