在Async Task BackGround Process中接收UDP数据包

时间:2013-07-03 12:16:07

标签: java android multithreading android-asynctask udp

protected String doInBackground(String... f_url) {
        int count;

        byte [] ip_bytes = new byte[] {(byte)192,(byte)168,(byte)1,(byte)100};
        try {
            InetAddress address = InetAddress.getByAddress(ip_bytes );

          byte buffer[] = new byte[2000];

                    DatagramPacket p = new DatagramPacket(buffer, buffer.length, address, port);
                    try {
                    DatagramSocket ds = new DatagramSocket(port);
                    Log.d("..........","Perfect Binding .... Waiting for Data");
                    ds.receive(p);
                    publishProgress(""+p);
                    Thread.sleep(100);

                    Log.d("","Received :) ");
                    } catch(Exception e)
                    {
                        e.printStackTrace();
                    }


        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return null;
    }

.................................... 这是在Async Backgroung Process中接收UDP数据包的代码,但我没有得到一个数据包......我的代码有什么问题?我怎么能克服这个问题?

1 个答案:

答案 0 :(得分:0)

似乎在接收时,您不需要在DatagramPacket中指定ip和端口。

DatagramPacket p = new DatagramPacket(buffer, buffer.length);