UPD packets doesn't arrive on a Samsung Galaxy Tab 7.7 but they arrive on a HTC Desire

断了今生、忘了曾经 提交于 2019-12-11 07:09:57

问题


I'm developing an Android 3.1 Tablet application.

I'm going to use this app to listen to UDP packets send by a device which is sending UDP packets to 255.255.255.255:8001 every 5 seconds.

Using desktop program Docklight scripting v1.9 I see that this device sends a 11 bytes packet every 5 seconds, by my app doesn't receive every packet: sometimes it receives one, and sometimes it receives 5 or 6 packets at the same time.

This is my Activity:

public class UDPSocketActivity extends Activity
{
    private UDPServerThread myThread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();

        TextView txt = (TextView)findViewById(R.id.deviceIP);
        txt.setText(Integer.toString(ipAddress));
    }

    public void onStartClick(View view)
    {
        Log.v("UDPSocketActivity", "onClick");

        try
        {
            myThread = new UDPServerThread("X", 8001);
            myThread.start();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This is UDP socket thread:

public class UDPServerThread extends Thread
{
    private static final int MESSAGE_SIZE = 11;
    protected DatagramSocket socket = null;
    protected boolean end = false;

    public UDPServerThread(String serverName, int port) throws IOException
    {
        super(serverName);

        Log.v("UDPServerThread", "constructor");

        socket = new DatagramSocket(port);
        socket.setBroadcast(true);
    }

    public void run()
    {
        while (!end)
        {
            try
            {
                byte[] buf = new byte[MESSAGE_SIZE];

                // Wait an incoming message.
                DatagramPacket packet = new DatagramPacket(buf, buf.length);

                Log.v("UDServerThread", "Listenning...");

                socket.receive(packet);

                Log.v("UDServerThread", "Mensaje recibido.");
            }
            catch (IOException e)
            {
                if (!socket.isClosed())
                    e.printStackTrace();
            }
        }
    }

    public void stopServer()
    {
        Log.v("UDPServerThread", "stopServer");
        if (socket != null)
            socket.close();
        end = true;
    }
}

This is AndroidManifest.xml permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

UPDATE:
If I send a UDP packet to Tablet's IP, e.g. UDP:192.168.1.135:8001, sometimes it receives the packet. And sometimes it receives three or four at the same time.

But if I send direct UDP packet to an HTC Desire 2.2.2 it receives all of them, but my HTC doesn't receive broadcast packets. And I'm using the same code.

This how I am receiving UDP broadcast packets (look at the time):

07-06 12:08:56.580: V/UDServerThread(6449): Mensaje recibido.
07-06 12:08:59.655: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:02.410: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.230: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.435: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.745: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.945: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:04.460: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:04.770: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:04.975: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:46.855: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.005: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.310: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.515: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.825: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:07.335: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:07.640: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:07.845: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:10.415: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:17.065: V/UDServerThread(6449): Mensaje recibido.

What am I doing wrong? Maybe I need some custom configuration.

By the way, I am testing it on a Samsung Galaxy Tab 7.7 with Android 3.1.


回答1:


If you want to make sure that you will receive all send packets you should use TCP protocol instead of UDP.




回答2:


To receive broadcasts on the HTC you need to get a Wifi Multicast lock. Basically some manufacturers are disabling receiving multicast by default. Still no idea what is happening on your Tablet, maybe some sort of buffering or slow piece of networking equipment in the middle?

WifiManager wifi;
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock ml = wifi.createMulticastLock("just some tag text");
ml.acquire();

When the asynctask stops I do a 
ml.release();

Code from Android Issues Tracker




回答3:


There was a problem with my Router.

Note: I answer my own question because I think it's useful for other people with the same problem (I was dealing with it by four days).



来源:https://stackoverflow.com/questions/11358552/upd-packets-doesnt-arrive-on-a-samsung-galaxy-tab-7-7-but-they-arrive-on-a-htc

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