• java 中多播、广播编程


    在 IP 协议层,有多播、广播的概念。IP 地址分为网络地址和主机地址,主机地址全部为1,就是广播地址。多播使用 D 类地址,从 224.0.0.0 到 239.255.255.255。IP 多播需要 IGMP 的支持,大致是,需要接收多播的主机先向路由器进行注册,然后路由器在收到多播包后会转发给注册的主机。

     java 语言实现多播、广播要用到 MulticastSocket,DatagramSocket。

    多播发送:

    package com.zhang;
    
    import java.net.DatagramPacket;
    import java.net.InetAddress;
    import java.net.MulticastSocket;
    
    public class Multicast_Send {
        public static void main(String[] args) {
            String[] message = { "hello", "hi" };
            int port = 1234; // 端口
            InetAddress group = null; // 地址
            MulticastSocket multicastSocket = null; // 组播套接字
    
            try {
                group = InetAddress.getByName("239.1.1.1"); // 设置多播地址
                multicastSocket = new MulticastSocket(port); 
                // 设置数据包的ttl,值从0到255,应该是和ip包的ttl字段对应上了
                // 之前设置为1,在无限局域网中,数据包无法到达其他主机,所以这里设为2
                // 如果设为0,则只能发送给本机
                multicastSocket.setTimeToLive(2); 
                multicastSocket.joinGroup(group);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            while (true) {
                try {
                    DatagramPacket packet = null;
                    for (String msg : message) // 循环发送每条广播信息
                    {
                        msg = msg + "," + System.currentTimeMillis();
                        byte buff[] = msg.getBytes();
                        packet = new DatagramPacket(buff, buff.length, group, port);
                        multicastSocket.send(packet);
                        System.out.println("send:" + msg);
                        Thread.sleep(2000);
                    }
                } catch (Exception e) {
                    System.out.println("Error:" + e);
                }
            }
            
        }
    }

    多播接收:

    package com.zhang;
    
    import java.net.DatagramPacket;
    import java.net.InetAddress;
    import java.net.MulticastSocket;
    
    public class Multicast_Recv {
        public static void main(String[] args) throws Exception {
            InetAddress group = InetAddress.getByName("239.1.1.1");
            int port = 1234;
            MulticastSocket socket = new MulticastSocket(port);
            socket.joinGroup(group);
    
            while (true) {
                byte buff[] = new byte[8192];
                DatagramPacket packet = null;
                packet = new DatagramPacket(buff, buff.length, group, port);
                socket.receive(packet);
                String message = new String(packet.getData(), 0, packet.getLength());
                System.out.println("receive:" + message);
            }
        }
    }

    广播发送:

    package com.zhang;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    public class Broadcast_Send {
        public static void main(String[] args) {
            // 本机ip:10.3.80.248, 子网掩码:255.255.255.0,推出广播地址:10.3.80.255
            String host = "10.3.80.255"; // 广播地址
            int port = 9999; // 广播的目的端口
            String message = null; // 用于发送的字符串
            try {
                while (true) {
                    message = "hello world " + System.currentTimeMillis();
                    InetAddress addr = InetAddress.getByName(host);
                    DatagramSocket ds = new DatagramSocket();
                    DatagramPacket dp = new DatagramPacket(message.getBytes(),
                            message.length(), addr, port);
                    ds.send(dp);
                    Thread.sleep(2000);
    //                ds.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    广播接收:

    package com.zhang;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    public class Broadcast_Recv {
        public static void main(String[] args) {
            int port = 9999; // 开启监听的端口
            DatagramSocket ds = null;
            DatagramPacket dp = null;
            byte[] buf = new byte[1024];// 存储发来的消息
            try {
                // 绑定端口
                ds = new DatagramSocket(port);
                dp = new DatagramPacket(buf, buf.length);
                System.out.println("监听广播端口打开:");
                
                while (true) {
                    ds.receive(dp);
                    System.out.println("收到广播消息:" + new String(buf));
                }
                
    //            ds.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Git: git tag 使用小结(给发布版本打标记,切换并修改某个历史版本)
    android: Android水波纹点击效果
    Flutter: 运行新项目报错
    FFmpeg编译:mac下编译iOS平台的FFmpeg库(支持armv7, arm64, i386, x86_64)
    Xcode: Xcode中Command Line Tools的安装方法
    win10: windows+E 改回打开我的电脑
    DialogFragment: DialogFragment的一些理解
    Android Studio: 查看SDK源代码
    HandlerThread: HandlerThread的理解
    Looper: Looper,Handler,MessageQueue三者之间的联系
  • 原文地址:https://www.cnblogs.com/allenwas3/p/8882204.html
Copyright © 2020-2023  润新知