zoukankan      html  css  js  c++  java
  • UDP 广播 Java

    1、服务端

    public class UdpBroadcastServer {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            // TODO Auto-generated method stub
            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);
                    int i;
                    StringBuffer sbuf = new StringBuffer();
                    for (i = 0; i < 1024; i++) {
                        if (buf[i] == 0) {
                            break;
                        }
                        sbuf.append((char) buf[i]);
                    }
                    System.out.println("收到广播消息:" + sbuf.toString());
                    try {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            catch (SocketException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    2、客户端

    public class UdpBroadcastClient {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            // TODO Auto-generated method stub
            // 广播的实现 :由客户端发出广播,服务器端接收
            String host = "255.255.255.255";// 广播地址
            int port = 9999;// 广播的目的端口
            String message = "test";// 用于发送的字符串
            try {
                InetAddress adds = InetAddress.getByName(host);
                DatagramSocket ds = new DatagramSocket();
                DatagramPacket dp = new DatagramPacket(message.getBytes(),
                        message.length(), adds, port);
                while (true) {
                    ds.send(dp);
                    try {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            catch (UnknownHostException e) {
                e.printStackTrace();
            }
            catch (SocketException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    SSM项目搭建(提供源码)
    U盘启动安装linux时卡在“starting dracut initqueue hook”
    nginx 中只能访问根目录,无法访问路由(404)
    在多GPU情况下TensorFlow如何指定在哪些GPU上运行程序
    TensorFlow只训练部分参数
    python中的随机数函数
    Python中读取、显示和保存图片的方法
    神经网络中参数数量的计算
    排序算法
    window Linux 双系统安装
  • 原文地址:https://www.cnblogs.com/diyishijian/p/5125552.html
Copyright © 2011-2022 走看看