与MulticastSocket中的Java的Andr​​oid的问题 [英] Problem with MulticastSocket on Java-Android

查看:166
本文介绍了与MulticastSocket中的Java的Andr​​oid的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始code用的MulticastSocket,试图与一个客户端和发送邮件服务器的简单的应用程序。

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

在code我对服务器:

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.

任何想法有什么不对?

推荐答案

迭戈,

在默认情况下,Android的无线网络协议栈筛选出多播数据包。看看<一href="http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html">http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html.

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.

您需要的线沿线的东西:

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 */

  }

这篇关于与MulticastSocket中的Java的Andr​​oid的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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