PS:禁止拷贝形式转载,转载请以URL形式
1.简介
使用java 实现多网卡下 组播的监听 与发送消息。只查询到只有监听多网卡例子,补充下多网卡下的发送。
2.参考
https://blog.csdn.net/u012134942/article/details/109231666
https://blog.csdn.net/lizefeng1998/article/details/121201524
3.环境
C:\Users\Administrator>java -version
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.4+11)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.4+11, mixed mode)
4.组播IP地址
224.0.0.0~239.255.255.255 所有组播地址
224.0.0.0~224.0.0.255 有特殊用途的组播地址(不能被路由)
224.0.0.1 同一网段所有主机
224.0.0.2 同一网段所有组播路由器
224.0.1.0~238.255.255.255 公网组播地址
239.0.0.0~239.255.255.255 私网组播地址
5.验证网卡是否加入组播
-
windows: 执行
image.pngnetsh interface ipv4 show joins
-
Linux: 执行
image.pngnetstat -g
6.实现
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* @ClassName
* @Description
* @Author dyf
* @Date 2022/5/20
* @Version 1.0
*/
public class MulticastDemo {
private static int PORT = 5555;
private static InetSocketAddress MULTICAST_ADDRESS = new InetSocketAddress("239.0.0.1", 0);
public static void main(String[] args) throws IOException {
System.out.println("#1 获取本机有效网卡");
Iterator<NetworkInterface> iterator = NetworkInterface.getNetworkInterfaces().asIterator();
Set<NetworkInterface> niSet = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false).filter(ni -> {
try {
boolean filter = ni.isUp() && !ni.isLoopback();
if (filter) System.out.printf("\t##1获取网卡:[%s] ip:%s \n", ni.getName(), ni.getInterfaceAddresses());
return filter;
} catch (SocketException e) {
e.printStackTrace();
return false;
}
}).collect(Collectors.toSet());
System.out.println("#2 监听所有网卡组播消息");
MulticastSocket server = new MulticastSocket(PORT);
for (NetworkInterface networkInterface : niSet) {
System.out.printf("\t##2监听网卡:%s \n", networkInterface.getName());
//joinGroup 这个方法只用到了 ip属性 并没有使用 port 属性,这个port 对于 socket 可以任意指定
server.joinGroup(MULTICAST_ADDRESS, networkInterface);
}
new Thread(() -> {
final byte[] buf = new byte[256];
while (true) {
try {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
server.receive(packet);
String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());
System.out.printf("\t##2监听网卡数据 客户端:[%s] 数据:[%s] \n", packet.getSocketAddress(), msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
System.out.println("#3 发送所有网卡组播消息");
MulticastSocket socket = new MulticastSocket();
for (NetworkInterface networkInterface : niSet) {
String msg = "msg" + networkInterface.getName();
byte[] bytes = msg.getBytes();
System.out.printf("##3发送消息 使用网卡:[%s] 消息:[%s] \n", networkInterface.getName(), msg);
//socket 会使用赋值网卡进行发送消息,不指定的话默认使用本机某张网卡
socket.setNetworkInterface(networkInterface);
socket.send(new DatagramPacket(bytes, bytes.length, MULTICAST_ADDRESS.getAddress(), PORT));
}
}
}
image.png
网友评论