MulticastSocket

MulticastSocket可以将数据报以广播的方式发送到多个客户端。
MulticastSocket有如下3个构造方法
public MulticastSocket():使用本机默认地址,随机端口来创建MulticastSocket对象。
public MulticastSocket(int portNumber):使用本机默认地址,指定端口创建MulticastSocket对象。
public MulticastSocket(SocketAddress bindaddr):使用本机指定IP地址,指定端口来创建MulticastSocket对象。
创建MulticastSocket对象后,还需要将MulticastSocket加入到指定的多点广播地址,MulticastSocket使用joinGroup()方法加入指定组;使用leaveGroup()方法脱离一个组。
joinGroup(InetAddress addr);
leaveGroup(InetAddress addr);
 
InetAddress 类没有提供构造,而提供如下两个静态方法来获取InetAddress对象
getByName(String host):如getByName("localhost"),getByName(www.baidu.com);
getByAddress(byte []addr):如getByAddress(new byte[]{127,0,0,1});
MulticastSocket类中有一个setTimeToLive(int ttl),当ttl为0时,指定数据报应停留在本地主机,为1时,指定数据报发送到本地局域网网,为32时,发送到本站点的网络上。为64时,发送到本地区,128时,发送到本大洲,255为全球。默认情况下,该ttl的值为1。


IP协议为多点广播提供了这批特殊的IP地址,这些IP地址的范围是224.0.0.0至239.255.255.255。

package hb.brodcast;

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

public class MulticastListener {
	private int port;
	private String host;

	public MulticastListener(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public void listen() {
		byte[] data = new byte[256];
		try {
			InetAddress ip = InetAddress.getByName(this.host);
			MulticastSocket ms = new MulticastSocket(this.port);
			ms.joinGroup(ip);
			DatagramPacket packet = new DatagramPacket(data, data.length);
			//receive()是阻塞方法,会等待客户端发送过来的信息
			ms.receive(packet);
			String message = new String(packet.getData(), 0, packet.getLength());
			System.out.println(message);
			ms.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		int port = 1234;
		String host = "224.0.0.1";
		MulticastListener ml = new MulticastListener(host, port);
		while (true) {
			ml.listen();
		}
	}
}

package hb.brodcast;

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

public class MulticastSender {
	private int port;
	private String host;
	private String data;

	public MulticastSender(String data, String host, int port) {
		this.data = data;
		this.host = host;
		this.port = port;
	}

	public void send() {
		try {
			InetAddress ip = InetAddress.getByName(this.host);
			DatagramPacket packet = new DatagramPacket(this.data.getBytes(), this.data.length(), ip, this.port);
			MulticastSocket ms = new MulticastSocket();
			ms.send(packet);
			ms.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		int port = 1234;
		String host = "224.0.0.1";
		String data = "hello world.";
		System.out.println(data);
		MulticastSender ms = new MulticastSender(data, host, port);
		ms.send();
	}
}
---------------
/**
 * ServerMulticastSocketTest.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-1-9 下午3:22:01
 */
package com.udpdemo.multicast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO 
 * @author cuiran
 * @version 1.0.0
 */
public class ServerMulticastSocketTest {

	/**
	 * 2014-1-9 下午3:22:01
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MulticastSocket multicastSocket;
		try {
			multicastSocket = new MulticastSocket();
			InetAddress address = InetAddress.getByName("239.0.0.1"); // 必须使用D类地址
	        multicastSocket.joinGroup(address); // 以D类地址为标识,加入同一个组才能实现广播
	        
	        while (true) {
	            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	            byte[] buf = time.getBytes();
	            DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
	            datagramPacket.setAddress(address); // 接收地址和group的标识相同
	            datagramPacket.setPort(10000); // 发送至的端口号
	            
	            multicastSocket.send(datagramPacket);
	            Thread.sleep(1000);
	        }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}

}

/**
 * ServerMulticastSocketTest.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-1-9 下午3:22:01
 */
package com.udpdemo.multicast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO 
 * @author cuiran
 * @version 1.0.0
 */
public class ServerMulticastSocketTest {

	/**
	 * 2014-1-9 下午3:22:01
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MulticastSocket multicastSocket;
		try {
			multicastSocket = new MulticastSocket();
			InetAddress address = InetAddress.getByName("239.0.0.1"); // 必须使用D类地址
	        multicastSocket.joinGroup(address); // 以D类地址为标识,加入同一个组才能实现广播
	        
	        while (true) {
	            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	            byte[] buf = time.getBytes();
	            DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
	            datagramPacket.setAddress(address); // 接收地址和group的标识相同
	            datagramPacket.setPort(10000); // 发送至的端口号
	            
	            multicastSocket.send(datagramPacket);
	            Thread.sleep(1000);
	        }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}

}

问题:

在android2.3下使用multicastsocket接收局域网组播数据的应用程序,在android2.3能正常工作,移植到了android4.0之后,每次从multicastsocket读取的数据都不超过512个字节,如果发送端发送操作512个字节的数据包,则只能收到512个字节。


方案:

1.每次DatagramPacket重新new个新的出来。
2.将原来的byte[],清空Arrays.fill(arr,(byte)0);

byte data[] = new byte[2048];
DatagramPacket dataPacket = new DatagramPacket(data, 2048);
在接收循环中,每次接收数据前调用
dataPacket .setData(dpData, 0, 2048);        
这样就没有问题了。



你可能感兴趣的:(android)