2 blo732 blo732 于 2016.02.26 11:35 提问

Android udp接收不到组播,请问如何解决? 50C

能接收到udp数据,但是接收不到udp组播数据,请各位大神帮帮忙,万分感谢!
代码如下:

package ncepu.wsy.udp_multicast_server;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint({ "SimpleDateFormat", "HandlerLeak" })
public class MulticastServer extends Activity {

private MulticastSocket ms;

private static String MulticastHost="238.9.9.1";

private static int PORT = 8302;

InetAddress receiveAddress;

String result = "";

private MulticastLock multicastLock;

LinearLayout layout;

String baotou="";
String mingling="";
String minglingleixing="";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    allowMulticast();
    setContentView(R.layout.activity_main);
    layout = (LinearLayout) findViewById(R.id.layout);

    new Thread(){
        public void run() {

            try {
                ms = new MulticastSocket(PORT);
                receiveAddress=InetAddress.getByName(MulticastHost);
                ms.joinGroup(receiveAddress);
            } catch (Exception e) {
                e.printStackTrace();
            }

            byte data[] = new byte[512];
            DatagramPacket dp = new DatagramPacket(data, 512);

            while (true) {
                try {
                    ms.receive(dp);
                    result = new String(data, 0, dp.getLength());
                    Message msg = new Message();
                    msg.what = 1;
                    hd.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        };
    }.start();


}
 Handler hd = new Handler(){
    @SuppressWarnings("unused")
    @Override
     public void handleMessage(Message msg)                                     
      {                                         
         super.handleMessage(msg);          
         switch(msg.what){
           case 1:
               SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日   HH:mm:ss");       
               Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间        
               String   str   =   formatter.format(curDate);  
               TextView tv =new TextView(MulticastServer.this);

               tv.setText("result--》"+result);

               layout.addView(tv);
               break; 
         }
      }                     
 };


private void allowMulticast() {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    multicastLock = wifiManager.createMulticastLock("multicast.test");
    multicastLock.acquire();
}

}

xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
package="ncepu.wsy.udp_multicast_server"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="16" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="ncepu.wsy.udp_multicast_server.MulticastServer"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4个回答

blo732
blo732   2016.02.26 13:35

为啥不是来回答问题,却是来找我拿代码,高手哪里去了?

u010941017
u010941017   2016.03.01 20:49

这个问题挺棘手的,我现在是ios可以接收PC的组播,Android手机可以接收组播,Android平板就是接收不到组播,而且这个平板可以发送组播到pc上,且同一款平板之前是可以接收到的。现在只能猜测是路由器不支持组播,或者路由器没有设置好。
以上是我遇到的问题,总之:1,组播锁 2,同网段 3,路由器 着重分析这几个方面吧

a521314963
a521314963   2017.05.09 18:15

你发的方法对吗?组播的地址与广播不一样

Mr_dsw
Mr_dsw   Ds   Rxr 2016.02.26 12:41

代码能发我一份么?dengshiwei_it@163.com

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!