UDP Broadcast: Motorola block incoming ports?

2019-05-07 06:17发布

I've created a library for sending and receiving UDP broadcasts using a WiFi network, and my code works fine, I tried it using a Nexus 5 and a Samsung Galaxy S2 and the communication works great, they both send and receive.

When I try my same code with a Moto G, the device can send packages to other phones, but can't receive anything. I can blame the Moto G because the code works great in other two devices and they both can receive the packages that the Moto G sends. I even tried two different Moto G, one stock and one rooted with specific firewall policies.

I tried using different ports, not many honestly, but I guess the problem is not there.

Any clues what could be wrong?

Android versions for each device: Nexus 5: 4.5 S2: 4.1.2 Moto G 1: 4.4.2 Moto G 2: 4.4.1 (not sure)

I'm targeting SDK 16. My code is here.

2条回答
Emotional °昔
2楼-- · 2019-05-07 06:54
  1. Use MulticastSocket for receiving instead of DatagramSocket.
  2. Send broadcast message to multicast address (224.0.0.0 to 239.255.255.255)

Sender

final String ip = "224.0.0.3";
final int port = 8091;
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(msg,msg.length, InetAddress.getByName(ip),port);
socket.send(packet);

Receiver

MulticastSocket socket = new MulticastSocket(8091);
socket.joinGroup(InetAddress.getByName("224.0.0.3"));
byte[] data = new byte[4096];
while(!stop){
    DatagramPacket packet = new DatagramPacket(data,data.length);
    socket.receive(packet);
}
查看更多
Ridiculous、
3楼-- · 2019-05-07 07:05

My Moto G (4.4.4) shows the same problem. Sending UDP-packets works, but receiving UDP-packets does not. I found several websites describing the same problem for several other vendors.

Workaround: I solved the problem for my Moto G, by using MulticastSocket (instead of DatagramSocket) with TTL=1 and IP=224.0.0.1.

查看更多
登录 后发表回答