简体   繁体   English

Android不接收广播UDP,但接收定向UDP

[英]Android not receiving broadcast UDP, but receiving directed UDP

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. 我有一个问题,我的Android设备(HTC Incredible运行库存2.3.4 rom)无法接收广播UDP,但是当我获得设备的IP(在同一网络上)并直接发送数据包对于IP,电话立即识别并解释它。 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. 我已经在两台不相关的PC上测试了WireShark,其中一台甚至没有在这个网络上的同一台交换机上,两者都能看到数据包(虽然他们确实称这个数据包“格式错误”)然而手机从未收到(它没有需要来自数据包的任何数据)所以我想知道发生了什么。

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? 那么我的手机是否出于某种原因拒绝UDP广播包,是否有任何方法可以测试它是否在手机上接收数据包? Is this the HTC Incredible issue HTC issue , and I need to program around this? 这是HTC难以置信的问题HTC问题 ,我需要围绕这个进行编程吗?

Thank you in advance for any help you can give me on this matter. 提前感谢您提供有关此事的任何帮助。

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. 答案有点迟了......但也许它会帮助那些有同样问题的人。 :) :)

Is this the HTC Incredible issue HTC issue 这是HTC难以置信的问题HTC问题

Looks very much like it. 看起来非常喜欢它。

and I need to program around this? 我需要围绕这个进行编程吗?

You can't, other than by relying on broadcast. 除了依靠广播,你不能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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