1. Multicast
a. Receiver:
public class MulticastReceive {
public static void main(String[] args) throws Exception {
listener();
}
public static void listener() throws Exception{
//组播地址
InetAddress group = InetAddress.getByName("224.0.0.1");
int port = 8888;
//创建组播套接字
MulticastSocket msr = null;
try {
msr = new MulticastSocket(port);
//加入连接
msr.joinGroup(group);
byte[] buffer = new byte[8192];
System.out.println("接收数据包启动!(启动时间: "+new Date()+")");
while(true){
//建立一个指定缓冲区大小的数据包
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
msr.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
//解码组播数据包
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(msr!=null){
try {
msr.leaveGroup(group);
msr.close();
} catch (Exception e2) {
}
}
}
}
}
b. Sending end
public class MulticastSender {
public static void server() throws Exception{
InetAddress group = InetAddress.getByName("224.0.0.1");//组播地址
int port = 8888;
MulticastSocket mss = null;
try {
mss = new MulticastSocket(port);
mss.joinGroup(group);
System.out.println("发送数据包启动!(启动时间"+new Date()+")");
while(true){
String message = "Hello "+new Date();
byte[] buffer = message.getBytes();
DatagramPacket dp = new DatagramPacket(buffer, buffer.length,group,port);
mss.send(dp);
System.out.println("发送数据包给 "+group+":"+port);
TimeUnit.SECONDS.sleep(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(mss!=null){
mss.leaveGroup(group);
mss.close();
}
} catch (Exception e2) {
}
}
}
public static void main(String[] args) throws Exception {
server();
}
}
2. Unicast
a. Receiver:
public class UnicastReceive {
// 演示udp通信接收端代码
public static void main(String[] args) throws Exception {
// 创建DatagramSocket对象(开放一个端口号)
DatagramSocket datagramSocket = new DatagramSocket(8888) ;
// 创建DatagramPacket对象(封装要接收的数据)
byte[] bytes = new byte[1024];
int len = bytes.length;
while (true) {
DatagramPacket datagramPacket = new DatagramPacket(bytes, len);
// 调用DatagramSocket方法完成数据的接收
System.out.println("接收数据开始................");
datagramSocket.receive(datagramPacket); // 这个方法是一个阻塞方法,如果没有发送端发送数据过来,线程一直处于阻塞状态
System.out.println("数据已接收................");
// 获取发送端的ip地址
InetAddress inetAddress = datagramPacket.getAddress();
String ip = inetAddress.getHostAddress();
// 解析数据
byte[] data = datagramPacket.getData(); // 获取数据
int length = datagramPacket.getLength(); // 获取数据的真实长度
System.out.println(ip + "接收数据:" + new String(data, 0, length, "UTF-8"));
}
// 释放资源
// datagramSocket.close();
}
}
b. Sending end:
public class UnicastSender {
// 演示udp通信发送端代码
public static void main(String[] args) throws Exception {
// 创建DatagramSocket对(无参构造方法)
DatagramSocket datagramSocket = new DatagramSocket() ;
// 创建DatagramPacket对象(封装要发送的数据,以及接收端的ip地址和端口号)
String s = "Hello,lalalala" ;
byte[] bytes = s.getBytes("UTF-8");
int len = bytes.length ;
InetAddress inetAddress = InetAddress.getByName("172.16.10.160");
int port = 32465 ;
DatagramPacket datagramPacket = new DatagramPacket(bytes , len , inetAddress , port) ;
// 调用DatagramSocket方法完成数据的发送
datagramSocket.send(datagramPacket);
// 释放资源
datagramSocket.close();
}
}
At present, the multicast test is ok, and there is a code snippet of multicast receiving data conversion json format: