在2020后最新版本中,由于udp 协议弃用了joinGroup(IntAddress)方法,我们只能采用第二种参数方法joinGroup(InetAddress mcastaddr) throws IOException,其中第一个参数是(组播地址,端口),创建对象
InetSocketAddress insa=new InetSocketAddress(“224.0.1.0”,“10000”);
一个第二个参数是本地的端口,我们在单独实现功能时使用该方法可以把第二个参数设置为null。
举个栗子:接收端
public static void main(String[] args) throws Exception {
//接收端
MulticastSocket ms=new MulticastSocket(10000);
InetSocketAddress in=new InetSocketAddress("224.0.1.0",10000);
DatagramPacket dp=new DatagramPacket(new byte[1024],1024);
ms.joinGroup(in,null);