Java-Android上的MulticastSocket问题

Die*_*loa 7 java android multicastsocket

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

我对服务器的代码:

    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);

    }

}
Run Code Online (Sandbox Code Playgroud)

这在客户端:

    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());
  }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我开始两者时,没有任何反应.客户端没有收到任何消息.

知道什么是错的吗?

小智 8

迭戈,

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

你需要的东西是:

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

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

    /* Process Multicast Packets */

  }
Run Code Online (Sandbox Code Playgroud)

  • 实际上,由于MulticastLock()方法没有公共构造函数,因此无法开箱即用.你需要这样做:WifiManager wm =(WifiManager)getSystemService(Context.WIFI_SERVICE); WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo"); multicastLock.acquire(); (感谢本网站澄清我的信息:http://www.whizzosoftware.com/forums/blog/1/entry-40-android-jmdns-and-wifi-multicast-packets/) (10认同)
  • 而且你的清单中也需要这个权限:<uses-permission android:name ="android.permission.CHANGE_WIFI_MULTICAST_STATE"/> (3认同)