Search code examples
androidsocketsudpdatagram

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


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.


Solution

  • 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).