Android不接收广播UDP,但接收定向UDP
我遇到一个问题,我的 Android 设备(运行 Stock 2.3.4 rom 的 HTC Incredible)无法接收广播 UDP,但是当我获取设备的 IP(在同一网络上)并直接发送数据包时对于IP,电话立即识别它并解释它。我已经在两台不相关的 PC 上使用 WireShark 进行了测试,其中一台甚至不在该网络上的同一交换机上,并且两台 PC 都能够看到该数据包(尽管他们确实称该数据包为“格式错误”),但手机从未收到(它没有需要数据包中的任何数据)所以我想知道发生了什么。
同样的事情也发生在模拟器中。
这是当前的代码。
m_UDPSocket = new DatagramSocket( 9000 );
m_UDPSocket.setBroadcast( true ); // unneeded?
boolean finished = false;
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
m_UDPSocket.setSoTimeout( 1000 );
do {
finished = true;
try
{
m_UDPSocket.receive(packet);
}
catch ( SocketTimeoutException e )
{
finished = false;
System.out.print( "Socket recieve timed out" );
}
} while( finished == false );
System.out.print( buf );
InetAddress address = packet.getAddress();
那么我的手机是否由于某种原因拒绝 UDP 广播数据包,有什么方法可以测试它是否在我的手机上接收数据包?这是 HTC Incredible 问题 HTC 问题吗?我需要围绕这个的程序?
预先感谢您就此事向我提供的任何帮助。
I'm having an issue where my Android device (An HTC Incredible running stock 2.3.4 rom) is not able to receive a broadcast UDP, but when I get the IP of the device (on the same network) and send the packet directly to the IP, the phone immediately recognizes it and interprets it. I have tested with WireShark on two unrelated PCs, one not even on the same switch on this network, and both are able to see the packet (though they do call the packet "malformed") Yet the phone never receives (it doesn't require any data from the packet) so I'm wondering what's happening.
The same thing is happening in the emulator as well.
This is the current code.
m_UDPSocket = new DatagramSocket( 9000 );
m_UDPSocket.setBroadcast( true ); // unneeded?
boolean finished = false;
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
m_UDPSocket.setSoTimeout( 1000 );
do {
finished = true;
try
{
m_UDPSocket.receive(packet);
}
catch ( SocketTimeoutException e )
{
finished = false;
System.out.print( "Socket recieve timed out" );
}
} while( finished == false );
System.out.print( buf );
InetAddress address = packet.getAddress();
So is my phone rejecting the UDP broadcast packets for some reason, any way to test that it is receiving the packet on my phone? Is this the HTC Incredible issue HTC issue , and I need to program around this?
Thank you in advance for any help you can give me on this matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 multicastlock 。看来您的手机忽略了不是直接发送给他的数据包(就像广播一样)。
答案有点晚了......但也许它可以帮助有同样问题的人。 :)
Try using a multicastlock . It seems that your phone is ignoring the packets that are not addressed to him directly (as a broadcast would be).
The answer is kind of late...but maybe it helps somebody that has the same issue. :)
看起来很像。
除了依靠广播之外,你不能这样做。
Looks very much like it.
You can't, other than by relying on broadcast.