单播

//[ 单播发送端 ]
public class Demo {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        DatagramSocket ds = new DatagramSocket(); //创建随机端口
        System.out.println("请输入要发送的内容: ");
        while(true){
            String s = sc.nextLine();
            if("886".equals(s)){
                System.out.println("退出成功");
                break;
            }
            byte[] bytes = s.getBytes();
            InetAddress byName = InetAddress.getByName("127.0.0.1");
            int port = 8888;
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length,byName,port);

            ds.send(dp);
        }
        ds.close();
    }
}
// [ 单播接收端 ]
public class Server {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(8888); //创建8888端口
        while (true) {
            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length);

            ds.receive(dp);
            byte[] data = dp.getData();
            int length = dp.getLength();
            System.out.println(new String(data,0,length));
        }
//        ds.close();
    }
}

组播

// [ 组播发送端 ]
public class Demo {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();

        String s = "hellow";
        byte[] bytes = s.getBytes();
        InetAddress address = InetAddress.getByName("224.0.1.0");
        int port = 10000;
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);

        ds.send(dp);

        ds.close();
    }
}
// [ 组播接收端 ] 
public class Server {
    public static void main(String[] args) throws IOException {
        MulticastSocket ms = new MulticastSocket(10000);
        DatagramPacket dp = new DatagramPacket(new byte[1024],1024);

        ms.joinGroup(InetAddress.getByName("224.0.1.0"));

        ms.receive(dp);

        byte[] data = dp.getData();
        int length = dp.getLength();
        System.out.println(new String(data,0,length));

        ms.close();
    }
}

广播

// [ 广播发送端 ]
public class Demo {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();

        String s = "广播";
        byte[] bytes = s.getBytes();
        InetAddress address = InetAddress.getByName("255.255.255.255");
        int port = 10000;
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);

        ds.send(dp);
        ds.close();
    }
}
// [ 广播接收端 ] 
public class Server {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10000);

        DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);

        ds.receive(dp);

        byte[] data = dp.getData();
        int length = dp.getLength();
        System.out.println(new String(data,0,length));

        ds.close();
    }
}
最后修改:2022 年 01 月 21 日
如果觉得我的文章对你有用,请随意赞赏