2

I am trying to create a simple app that will receive UDP packets from a WiFi module. The WiFi module sends a packet every 5 seconds, but I have been unable to receive one successfully.

Using the toasts as a way to debug, I receive them, in the correct order, up to "DIDN'T GET IT HERE EITHER", which tells me that the problem lies in the 'socket.receive(packet)' line.

Does anyone have any insight as to why it might not be working? I have also tried variations of the code by looking at a lot of questions/examples online, but they all still result in the problem with the 'socket.receive(packet)' line.

Also, I wrote a python script to do the same thing, look for UDP packets on the same port, and running it with QPython, it does receive the packets, so it seems that the packets are being sent by the WiFi module, and received by the tablet, it's just that the app won't read the data for some reason.

Any help or insight will be greatly appreciated, thank you.

Here is the code:

public void run() {

    Toast.makeText(this, "RIGHT BEFORE TRY", Toast.LENGTH_LONG).show();

    DatagramSocket socket;
    DatagramPacket packet;


   try {
        Toast.makeText(this, "IN THE TRY", Toast.LENGTH _LONG).show();
        socket = new DatagramSocket(9750);
        byte[] buf = new byte[1024]; //buffer
        socket.setSoTimeout(100000);
        Toast.makeText(this, "Timeout is: " + socket.getSoTimeout(), Toast.LENGTH_LONG).show();
        //DatagramPacket packet = new DatagaramPacket(buf, buf.length);
        //Toast.makeText(this, "PACKET SIZE IS: "+buf.length, Toast.LENGTH_LONG).show();

        // while(true) {
        Toast.makeText(this, "IN THE WILD!", Toast.LENGTH_LONG).show();
        packet = new DatagramPacket(buf, buf.length);
        Toast.makeText(this, "PACKET SIZE IS: " + buf.length, Toast.LENGTH_LONG).show();

        try {
            //socket.receive(packet);
            socket.receive(packet);
            Toast.makeText(this, "GOT SOMETHING!", Toast.LENGTH_LONG).show();
        }
        catch (Exception i) {
            // TODO Auto-generated catch block
            //i.printStackTrace();
            Toast.makeText(this, "DIDN'T GET IT HERE EITHER!", Toast.LENGTH_LONG).show();
        }
        //byte[] result = new byte[packet.getLength()];
        byte[] result = packet.getData();
        System.arraycopy(packet.getData(), 0, result, 0, packet.getLength());
        String msg = new String(result);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        Toast.makeText(this, "END WHILE!", Toast.LENGTH_LONG).show();
        //}

    } catch (Exception e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        Toast.makeText(this, "DIDN'T GET IT!", Toast.LENGTH_LONG).show();

    }

}

Update: After the first two answers, it looks like we were trying to do network operations in the main thread, which is not doable, so we are currently trying to implement it with another thread or an asynchronous task. Hopefully with some good news :)

  • 2
    Did you add `` to your AndroidManifest.xml? Also, could you please print out `i.toString()` in that `catch` block? – pelya May 22 '15 at 18:43
  • pelya, I do have the permission statement, and I just added the i.toString(). The result of the i.toString() is : 2!android.os.NetworkOnMainThreadException – Kevin Erskine May 22 '15 at 21:31

1 Answers1

0

As you said There is NetworkOnMainThread exception you just need to put this question on new thread and start it...

Thread thread = new Thread() {
    @Override
    public void run() {
Toast.makeText(this, "RIGHT BEFORE TRY", Toast.LENGTH_LONG).show();

    DatagramSocket socket;
    DatagramPacket packet;


   try {
        Toast.makeText(this, "IN THE TRY", Toast.LENGTH _LONG).show();
        socket = new DatagramSocket(9750);
        byte[] buf = new byte[1024]; //buffer
        socket.setSoTimeout(100000);
        Toast.makeText(this, "Timeout is: " + socket.getSoTimeout(), Toast.LENGTH_LONG).show();
        //DatagramPacket packet = new DatagaramPacket(buf, buf.length);
        //Toast.makeText(this, "PACKET SIZE IS: "+buf.length, Toast.LENGTH_LONG).show();

        // while(true) {
        Toast.makeText(this, "IN THE WILD!", Toast.LENGTH_LONG).show();
        packet = new DatagramPacket(buf, buf.length);
        Toast.makeText(this, "PACKET SIZE IS: " + buf.length, Toast.LENGTH_LONG).show();

        try {
            //socket.receive(packet);
            socket.receive(packet);
            Toast.makeText(this, "GOT SOMETHING!", Toast.LENGTH_LONG).show();
        }
        catch (Exception i) {
            // TODO Auto-generated catch block
            //i.printStackTrace();
            Toast.makeText(this, "DIDN'T GET IT HERE EITHER!", Toast.LENGTH_LONG).show();
        }
        //byte[] result = new byte[packet.getLength()];
        byte[] result = packet.getData();
        System.arraycopy(packet.getData(), 0, result, 0, packet.getLength());
        String msg = new String(result);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        Toast.makeText(this, "END WHILE!", Toast.LENGTH_LONG).show();
        //}

    } catch (Exception e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        Toast.makeText(this, "DIDN'T GET IT!", Toast.LENGTH_LONG).show();

    }


    }
};
thread.start();
  • Ekant Sharma, originally tried this without the timeout function since I understood it to be defaulted to infinite, but it still wasn't working so I added that to play with it. – Kevin Erskine May 22 '15 at 21:35