Android无法接收组播数据包 [英] Android can not receive multicast packet

查看:586
本文介绍了Android无法接收组播数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打得有点多播插座。余写,它发送一个消息到机器人客户机的服务器。直到目前,客户端应该只记录所接收的消息。 我注意到,没有一个组播数据包是我的设备上接收到的。

I'm playing a little bit with multicast sockets. I write a server which sends a message to a android client. Until yet the client should only log the received message. I noticed that no multicast packet are received on my device.

下面是code服务器(运行在PC):

Here is the code for the server (runs on the pc):

public class MulticastServer{

private int port;

private boolean running = false;

private MulticastSocket serverSocket;

private InetAddress group;

private String multicastAddress = "230.192.0.11";

public MulticastServer(int port) {
    super();
    this.port = port;
    init();
}

public MusicStreamerServer() {
    this(5500);
}

private void init() {

    try {
        group = InetAddress.getByName(multicastAddress);
        serverSocket = new MulticastSocket(port);
        serverSocket.joinGroup(group);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public void start() throws IOException {
    System.out.println("server started");

    if (running)
        return;

    running = true;

    new Thread(new Runnable() {

        @Override
        public void run() {

            byte[] buf = new byte[1024];

            DatagramPacket packet = new DatagramPacket(buf, buf.length,
                    group, port);

            String msg = "msg";


            while (running) {

                                    packet.setData(msg.getBytes(), 0, msg.length());


                try {
                    serverSocket.send(packet);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }).start();

}

public void stop() throws IOException {
    running = false;
} }

下面是code为Android客户端:

Here is the code for the android client:

public class MainActivity extends Activity {

private MulticastSocket socket;

private InetAddress group;

private String multicastAddress = "230.192.0.11";

private int port = 5500;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();
}

private void init() {

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();

    StrictMode.setThreadPolicy(policy);

    try {
        group = InetAddress.getByName(multicastAddress);
        socket = new MulticastSocket(port);
        socket.joinGroup(group);
        socket.setBroadcast(true);
    } catch (IOException e) {
        e.printStackTrace();
        Log.wtf("init", e.getMessage());
    }

    new Thread(new Runnable() {

        @Override
        public void run() {

            WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            WifiManager.MulticastLock multicastLock = wm
                    .createMulticastLock("mylock");

            multicastLock.acquire();

            byte[] buf = new byte[1024];

            while (true) {

                try {
                    socket.receive(packet);

                    Log.d("receiver","received = " + (new String(packet.getData())));

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }).start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

我测试过的code有2个不同的设备。在Nexus4和Nexus7(2013)都运行最新的Andr​​oid。

I've tested the code with 2 different devices. The Nexus4 and the Nexus7 (2013) both running the latest Android.

任何人可以帮助我吗?

感谢

推荐答案

请问您的清单要求适当的权限?

Does your manifest request the proper permissions?

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

此外,您可能希望与您的手机在WiFi菜单中的高级设置播放,支持Wi-Fi无线优化和保持无线网络连接睡眠时可能会影响你的能力做多播。

Also, you may want to play with the Advanced settings in the WiFi menu on your phone, both Wi-Fi optimizations and Keep Wi-Fi on during sleep may impact your ability to do multicasts.

这篇关于Android无法接收组播数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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