UDP广播:摩托罗拉阻止传入端口? [英] UDP Broadcast: Motorola block incoming ports?
问题描述
我已经创建了一个库,用于使用WiFi网络发送和接收 UDP广播,并且我的代码运行正常,我尝试了一下使用 Nexus 5 和三星Galaxy S2 ,并且通信效果很好,它们都可以发送和接收。
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.
当我使用 Moto G 尝试相同的代码时,设备可以将包裹发送到其他手机,但什么也收不到。我可以责怪Moto G,因为该代码在其他两个设备中都可以正常工作,而且它们都可以接收Moto G发送的程序包。我什至尝试了两种不同的Moto G,一种股票,另一种植根于特定的防火墙策略。
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版本:
Nexus 5:4.5
S2:4.1.2
Moto G 1:4.4.2
Moto G 2:4.4.1(不确定)
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)
我的目标是SDK16。我的代码在这里。
I'm targeting SDK 16. My code is here.
推荐答案
- 将
MulticastSocket
用于 - 将广播消息发送到多播地址
(224.0.0.0至239.255.255.255 <,而不是
DatagramSocket
。 / code>)
- Use
MulticastSocket
for receiving instead ofDatagramSocket
. - 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);
}
这篇关于UDP广播:摩托罗拉阻止传入端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!