简体   繁体   中英

Android UDP not receiving data

I am trying to get data from UDP server on my android application. This code is working fine in java application running from NetBeans but in android i am not getting any data from UDP server. I am actually confused because i didn't get any solution on internet for this problem. Any kind of help will be appreciated. Thank you

public class UDPService extends Service {

private String stringData = null;

byte[] buffer = new byte[8048];
DatagramPacket dp;
DatagramPacket packet;

public void onCreate(){
    super.onCreate();
    AsyncReceiveUdp2 asyncReceiveUdp2 = new AsyncReceiveUdp2();
    asyncReceiveUdp2.execute();
}


@Override
public IBinder onBind(Intent intent) {
    return  null;
}

public class AsyncReceiveUdp2 extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... f_url) {
        try {
            byte[] msg = new byte[1000];

            DatagramSocket ds = null;



            try {
                ds = new DatagramSocket(1050);
                packet = new DatagramPacket(buffer, buffer.length);
                ds.setBroadcast(true);
                dp = new DatagramPacket(msg, msg.length);
                dp.setPort(1050);
            }catch (IOException e){
                Log.d("df",e.getMessage());
            }
            catch (Exception e){

            }

            while(true) {
                try {
                    ds.receive(dp);
                }catch (IOException e){
                    e.printStackTrace();
                }
                Log.d("dfg","fg");
                Thread.sleep(700);
                stringData = new String(msg, 0, dp.getLength());
                String message= new String(buffer, 0, packet.getLength());
                String substr = "\\*";
                String[] parts = message.split(substr);
                String after = parts[1];
                String[] N = after.split ("\\,");
                String Ticket = N[0];
                String CT = N[1];
                System.out.println("Ticket No:" +N[0]);
                System.out.println ("Counter No:" +N[1]);
            }

            } 
           catch (Exception e) {
            e.printStackTrace();
        }
        return true;
       }
     }
   }

I expect that this is masking the real problem:

catch (Exception e){

}

That catches any other exceptions and throws away all evidence that it occurred. Don't do that. Ever. It makes your code hard to debug. Either let the unexpected exceptions propagate (don't catch them) or catch and log them.

Change it to:

catch (Exception e){
    Log.d("df", "unexpected exception", e);
}

and see if you get anything useful in your logcat. If not, then then next thing to check is if there is a firewall blocking the UDP packets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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