五、Android局域网多播(组播)

多播

组播使用UDP对一定范围内的地址发送相同的一组Packet,即一次可以向多个接受者发出信息,其与单播的主要区别是地址的形式。IP协议分配了一定范围的地址空间给多播(多播只能使用这个范围内的IP),IPv4中组播地址范围为224.0.0.0到239.255.255.255

###发送端

package com.turing.mobilecontrol;

import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

public class MobileSocketClient {
	private static MobileSocketClient mobileSocketClient = null;
	private static String BROADCAST_IP = "224.0.0.1";
	private static int BROADCAST_PORT = 9898;
	private static final String TAG = "MobileSocketClient";
	private MulticastSocket multicastSocket;
	private InetAddress inetAddress;
	private Handler handler;

	private MobileSocketClient() {
		try {
			//初始化组播
			inetAddress = InetAddress.getByName(BROADCAST_IP);
			multicastSocket = new MulticastSocket(BROADCAST_PORT);
			multicastSocket.setTimeToLive(1);
			multicastSocket.joinGroup(inetAddress);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void init(Handler handler) {
		this.handler = handler;
	}

	public static MobileSocketClient getInstance() {
		if (mobileSocketClient == null) {
			mobileSocketClient = new MobileSocketClient();
		}
		return mobileSocketClient;
	}

   //发送数据
	public void send(final String content) {
		new AsyncTask<String, Integer, String>() {

			@Override
			protected String doInBackground(String... paramVarArgs) {
				byte[] data = paramVarArgs[0].getBytes();
				//构造要发送的数据
				DatagramPacket dataPacket = new DatagramPacket(data,
						data.length, inetAddress, BROADCAST_PORT);
				try {
					multicastSocket.send(dataPacket);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return App.getInstance().getResources()
							.getString(R.string.send_failed);
				}
				return App.getInstance().getResources()
						.getString(R.string.send_success);
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				Message msg = new Message();
				msg.what = TuringHandler.STATUS;
				msg.obj = result;
				handler.sendMessage(msg);
			}
		}.execute(content);

	}

}

###接收端

package com.tuling.demo.socket;

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

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.tuling.demo.TuringHandler;

public class ServerSocket extends Thread {
	private static final String TAG = TuringServerSocket.class.getSimpleName()
			.toString();
	private static int BROADCAST_PORT = 9898;
	private static final String BROADCAST_IP = "224.0.0.1";
	private MulticastSocket multicastSocket;
	String content = "";
	private Handler handler;
	private InetAddress inetAddress;

	public TuringServerSocket(Handler handler) throws IOException {
		// 多播配置
		multicastSocket = new MulticastSocket(BROADCAST_PORT);
		inetAddress = InetAddress.getByName(BROADCAST_IP);
		multicastSocket.joinGroup(inetAddress);
		// handler
		this.handler = handler;
	}

	@Override
	public void run() {
		while (true) {
			Log.d(TAG, "run");
			byte buf[] = new byte[1024];
			DatagramPacket dp = new DatagramPacket(buf, buf.length,
					inetAddress, BROADCAST_PORT);
			try {
				multicastSocket.receive(dp);
				String content = new String(buf, 0, dp.getLength());
				Message msg = new Message();
				msg.what = TuringHandler.RECEIVE_COMMOND;
				msg.obj = content;
				handler.sendMessage(msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {

	}
}

逻辑都比较简单,假如到一个组,一个发送,批量接收
代码:http://download.csdn.net/detail/aa375809600/9717268 (0分下载 )

博主开发的第三方CSDN客户端.体验很棒哦,快来体验下载吧
在这里插入图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页