user1739999
user1739999

Reputation: 93

Android code not receiving UDP packet

This Android code is not receiving packets. The code is based on some java code that works fine. Can someone give input, what could be the issue. We are not running on the emulator so its not the emulator issues people have. The sender app is using the android tablet wi-fi tablet address as well as the udp port matches in both the sender and receiver.

Could it be the app is dropping packets.

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class NetworkServer extends Thread
{

   DatagramSocket mSocket = null;   
   boolean isFinish = false;

   private SimplestPossibleActivity activity;

   public NetworkServer(SimplestPossibleActivity activity)
   {
    this.activity = activity;
   }

   public void run() 
   {

      try 
      {

        Log.d("UDP", "Listening");
        mSocket = new DatagramSocket( 2010); //4444
        mSocket.setBroadcast(true);

        while (!isFinish) 
        {

           Log.d("UDP", "C: socket create success");
           byte[] recvbuffer = new byte[12];
           DatagramPacket packet = new DatagramPacket(recvbuffer,recvbuffer.length);
           Log.d("UDP", "receiving...");
           mSocket.receive(packet);
           Log.d("UDP", "received packet");

           ByteBuffer bb = ByteBuffer.allocate(recvbuffer.length).
                                       order(ByteOrder.LITTLE_ENDIAN);
           bb.put(recvbuffer);
           bb.rewind();
           //System.out.println(bb.getFloat());
           //System.out.println(bb.getFloat());
           //System.out.println(bb.getFloat());

           // byte[] buffer = packet.getData();
           //  String msg = new String(buffer);

           // bundle data to send to message handler
           Bundle data = new Bundle();
           data.putFloat("latitude",  bb.getFloat());
           data.putFloat("longitude", bb.getFloat());
           data.putFloat("altitude",  bb.getFloat());

           Message msgHandle = new Message();
           msgHandle.setData(data);
           mhandler.sendMessage(msgHandle);

       } //end while
     } catch (Exception e) {
         Log.e("UDP", "C: Error", e);
     }

   }

   private Handler mhandler = new Handler() 
   {

        @Override
        public void handleMessage(Message msg) 
        {
           float lat;
           float lon;
           float alt;

           Bundle data = msg.getData();
           Log.d("NetworkServer","adding position" + "lat = " + data.getFloat("latitude") +
                                 "lon = " + data.getFloat("longitude") + 
                                 "alt = " + data.getFloat("altitude"));
           activity.addPosition(data.getFloat("latitude"), 
                               data.getFloat("longitude"), 
                               data.getFloat("altitude"));

    }

   };
}

Upvotes: 0

Views: 1286

Answers (1)

dubmojo
dubmojo

Reputation: 6838

Well I've seen Android not accept UDP packets that were definitely broadcast. I'm still trying to determine the issue.

Your server code included should be fine. The recvbuffer is the right size for the expected data, and I'm assuming you would have explained any experienced errors if it was actually receiving the UDP packet. Meaning, whatever your use case is, the UDP packet isn't being received by the server because it wasn't sent, or it was interrupted.

1) I believe you're using the sender app on a PC, not an Android device. ("The sender app is using the android tablet wi-fi tablet address as well as the udp port matches in both the sender and receiver.") If this is the case, install Wireshark and listen for the sent UDP packets. Confirm that the packets are indeed being sent.

2) I'm assuming that you didn't test the original Java code from the same PC. e.g. the receiver and sender are on the same PC. Are you testing the sender on a PC to the Android receiver app?

3) I'm also assuming that you have the correct minimum Android permissions in your AndroidManifest.xml file. e.g. If you're multicasting UDP, you'll need to do more than this, but I don't believe you are.

The only way to claim that the packets are dropped would be to determine they were actually sent into the network. (Wireshark it if possible) Your packet size is so small that there is no reason to think any router would drop the packet based on size limitations.

Try submitting the client code and explain how your test scenario works exactly.

Upvotes: 1

Related Questions