UDP Broadcast: Motorola block incoming ports?

被刻印的时光 ゝ 提交于 2019-12-12 09:42:04

问题


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.


回答1:


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.




回答2:


  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);
}


来源:https://stackoverflow.com/questions/27319840/udp-broadcast-motorola-block-incoming-ports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!