简体   繁体   中英

Java UDP DatagramSocket does not receive DatagramPacket

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数据包

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM