简体   繁体   English

Android UDP 未收到数据

[英]Android UDP not receiving data

I am trying to get data from UDP server on my android application.我正在尝试从 android 应用程序上的 UDP 服务器获取数据。 This code is working fine in java application running from NetBeans but in android i am not getting any data from UDP server.此代码在从 NetBeans 运行的 java 应用程序中运行良好,但在 android 中,我没有从 ZF5EF036B4D8B639FBC823ZE 服务器获取任何数据。 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.看看你的 logcat 中是否有任何有用的东西。 If not, then then next thing to check is if there is a firewall blocking the UDP packets.如果没有,那么接下来要检查的是是否有防火墙阻止了 UDP 数据包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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