Java-Android 上的 MulticastSocket 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4589946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:06:37  来源:igfitidea点击:

Problem with MulticastSocket on Java-Android

javaandroidmulticastsocket

提问by Diego Ulloa

I'm starting to code with MulticastSocket, trying to make a simple app with a client and a server to send messages.

我开始使用 MulticastSocket 进行编码,尝试使用客户端和服务器来制作一个简单的应用程序来发送消息。

The code I have for the server:

我的服务器代码:

    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.InetAddress;
    import java.net.MulticastSocket;
    import java.net.SocketException;


    public class Servidor {
 private static MulticastSocket ms;
 public static void main(String[] args) throws IOException{

  InetAddress sessAddr = InetAddress.getByName("224.2.76.24");
     try{
    sessAddr = InetAddress.getByName("224.2.76.24");
       ms = new MulticastSocket(5500);
       ms.joinGroup(sessAddr);

       while (true)
       {
       byte[] mensaje = new byte[1024];
       mensaje = "aa".getBytes();
       DatagramPacket dp = new DatagramPacket(mensaje, mensaje.length,sessAddr,5500);
       ms.send(dp);
       }
      }
      catch (SocketException se) {
        System.err.println(se);
      }

      ms.leaveGroup(sessAddr);

    }

}

And this on the client:

这在客户端上:

    package com.example;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.InetAddress;
    import java.net.MulticastSocket;
    import java.net.UnknownHostException;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.EditText;
    import android.widget.TextView;

    public class ClienteMultiCast extends Activity {
    /** Called when the activity is first created. */


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView Mensaje;
        Mensaje =(TextView)findViewById(R.id.Mensaje);


        InetAddress ia = null;
        byte[] buffer = new byte[65535];
        MulticastSocket ms = null;
        int port = 5500;
        try {
        ia = InetAddress.getByName("224.2.76.24");
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length,ia,port);
        ms = new MulticastSocket(port);
            ms.joinGroup(ia);
            while (true) {
                ms.receive(dp);
                String s = new String(dp.getData(),0,dp.getLength());
                Mensaje.setText(s);
            }

            } catch (UnknownHostException e) {Mensaje.setText(e.getMessage());} catch (IOException e) {Mensaje.setText(e.getMessage()); }

            try {
            ms.leaveGroup(ia);
             } catch (IOException e) {
            Mensaje.setText(e.getMessage());
  }
    }
}

The problem is that when I start both, nothing happens. The client doesn't get any message.

问题是,当我同时启动两者时,什么也没有发生。客户端没有收到任何消息。

Any idea what's wrong?

知道出了什么问题吗?

回答by Tim

Diego,

迭戈,

By default, the Android WiFi stack filters out multicast packets. Take a look at http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html.

默认情况下,Android WiFi 堆栈会过滤掉多播数据包。看看http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html

You need something along the lines of:

你需要一些类似的东西:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Turn off multicast filter */
    MulticastLock mcastLock = new MulticastLock();
    mcastLock.acquire();

    /* Process Multicast Packets */

  }

回答by William

It appears that Multicast support in Android is not as solid as some of use might hope. See http://codeisland.org/2012/udp-multicast-on-android/

Android 中的多播支持似乎并不像某些用户希望的那样可靠。见http://codeisland.org/2012/udp-multicast-on-android/

Ie whether it actually works out or may be device dependent. It is not working on my Nexus5.

即它是否真的有效或可能取决于设备。它不适用于我的 Nexus5。

https://code.google.com/p/android/issues/detail?id=51195

https://code.google.com/p/android/issues/detail?id=51195