android udp接收

address=InetAddress.getByName(url);
    int i=0;
    byte[] receiveByte = new byte[1316];
    Log.i("2015-6-3","thread running");
    dataPacket = new DatagramPacket(receiveByte, receiveByte.length);
    if(address==NULL)
    {
     try {
        dataSocket = new DatagramSocket(PORT);
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        Log.i("2015-6-3","port cannot open");
        e1.printStackTrace();
    }}else{
        try {
            dataSocket = new DatagramSocket(PORT,address);
        } catch (SocketException e1) {
            // TODO Auto-generated catch block
            Log.i("2015-6-8","port cannot open");
            e1.printStackTrace();
    }}
        while(true)
        {
            try {
                dataSocket.receive(dataPacket);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.i("2015-6-3","packet receive failed");
                e.printStackTrace();
            }

相关代码如上,其中UDP只是监听端口,数据接收没有问题。但是加上IP就直接闪退到上一个activity,求解答。InetAddress.getByName(url)内的url是“239.255.x.x”格式的

----------分割线-------------
经过修改后的代码

    swi=1;
    try {
        dataaddr=new MulticastSocket(port);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        address = InetAddress.getByName(url);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        dataaddr.joinGroup(address);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
try {
                    dataaddr.receive(dataPacket);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.i("2015-6-3","packet receive failed");
                    e.printStackTrace();
                }.........

这时出现了一个bug,
W/ContextImpl( 1909): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1168 com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeQueueing:4109 com.android.server.wm.InputMonitor.interceptKeyBeforeQueueing:357 com.android.server.input.InputManagerService.interceptKeyBeforeQueueing:1352 dalvik.system.NativeStart.run:-2

不知道有没有解,PS:VLC或者ffmpeg都是用c接收这种UDP包的

阅读 7.5k
2 个回答
        MulticastSocket dataaddr=new MulticastSocket(1234);
        InetAddress address = InetAddress.getByName("239.0.0.1");
        dataaddr.joinGroup(address);
        byte[] receiveByte = new byte[1316];
        dataPacket = new DatagramPacket(receiveByte, receiveByte.length);
        try {
                    dataaddr.receive(dataPacket);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.i("2015-6-3","packet receive failed");
                    e.printStackTrace();
                }

以上为解决办法,谢谢@huandu

DatagramSocket(int aPort, InetAddress addr) 这里的 addr 只能绑定本机的 IP,你给的 IP 恐怕不是 Android 机器自己的 IP 吧。

如果 addr 不是本机 IP,new DatagramSocket(PORT,address) 会抛异常,从而导致 dataSocket == null,后面你在 dataSocket.receive 调用时没有捕捉 NullPointerException,所以直接挂掉了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题