没有收到组播包的Andr​​oid设备 [英] Android device not receiving multicast package

查看:160
本文介绍了没有收到组播包的Andr​​oid设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个聊天应用程序,其中Android的客户端将交换各自的IP的使用多播(UDP)。

I am developing a chat application where Android clients will exchange their IP's using multicasting(UDP).

每个设备将在一个单独的线程中发送它的IP到多个客户端(所有运行此应用程序的设备)。将有另一个接收器线程,会听这些组播数据包。这是我的code。

Every device will send its ip to multiple clients(all the devices running this app) in one separate Thread. There will be another receiver thread which will listen to these multicast packets. Here is my code.

//组播code。

DatagramSocket socket = new DatagramSocket(9898);
            byte buff[] = ip.getBytes();
            DatagramPacket packet = new DatagramPacket(buff, buff.length, InetAddress.getByName("224.0.0.1"),9999);
            socket.send(packet);
            socket.close();

//接收器code

//Receiver code

MulticastSocket socket = new MulticastSocket(9999);
        InetAddress group = InetAddress.getByName("224.0.0.1");
        socket.joinGroup(group);

        DatagramPacket packet;

            byte[] buf = new byte[256];
            byte  b = 'x'; //just a separator for time being
            Arrays.fill(buf,b);
            packet = new DatagramPacket(buf, buf.length);
            String received= "";
            while(received!=null)
            {
                socket.receive(packet);
                received = new String(packet.getData());
                received = received.substring(0,received.indexOf('x'));
                this.setIp(received);
                System.out.println("Address: " + received);
            }

        socket.leaveGroup(group);
        socket.close();

现在的问题是每一个设备的打印自己的地址。现在看来,这从来不听其他组播包(我的意思是要打印其他IP的为好)。我也得到了下面的日志,不知道这是相关的。

The problem is every device prints its own address. It seems it never listens to other multicast packages(I mean it should print other ip's as well). I also get a below log, not sure if that's related.

11-04 23:56:17.985: I/OSNetworkSystem(603): mcastAddDropMembership interfaceIndex=0

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

您需要获得一个<一个href="http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html">MulticastLock在您的应用程序,这将使您的应用程序接收所有没有明确给该设备在网络上的数据包。

You need acquire a MulticastLock in your app, which will allow your app receive packets that are not explicitly addressed to this device on the network.

所需的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

样品code:

Sample code:

// Acquire multicast lock
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

//Do some mutlicast job here
... ...

// Once your finish using it, release multicast lock
if (multicastLock != null) {
    multicastLock.release();
    multicastLock = null;
}

这篇关于没有收到组播包的Andr​​oid设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆