zoukankan      html  css  js  c++  java
  • UDP通信 Java

    public class UdpServerTest {
    
        /**
         * @param args
         * @throws SocketException
         */
        public static void main(String[] args) throws SocketException {
    
            final DatagramSocket server = new DatagramSocket(41017);
            new Thread(new Runnable() {
    
                @Override
                public void run() {
    
                    try {
                        int port = 41216;
                        InetAddress addr = InetAddress.getByName("10.1.31.156");
                        int i = 1;
                        while (true) {
                            // TODO Auto-generated method stub
                            String sendStr = "Hello ! I'm Server";
                            sendStr = sendStr + " " + i;
                            byte[] sendBuf;
                            sendBuf = sendStr.getBytes();
                            DatagramPacket sendPacket = new DatagramPacket(sendBuf,
                                    sendBuf.length, addr, port);
                            server.send(sendPacket);
                            i++;
                            Thread.sleep(1000);
                        }
                    }
                    catch (SocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (UnknownHostException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
    
                @Override
                public void run() {
    
                    // TODO Auto-generated method stub
                    try {
                        byte[] recvBuf = new byte[1000];
                        DatagramPacket recvPacket = new DatagramPacket(recvBuf,
                                recvBuf.length);
                        while (true) {
                            server.receive(recvPacket);
                            String recvStr = new String(recvPacket.getData(), 0,
                                    recvPacket.getLength());
                            System.out.println("收到:" + recvStr);
                            Thread.sleep(1000);
                        }
                    }
                    catch (SocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    public class UdpClientTest {
    
        /**
         * @param args
         * @throws SocketException
         */
        public static void main(String[] args) throws SocketException {
    
            final DatagramSocket client = new DatagramSocket(41216);
            // TODO Auto-generated method stub
            new Thread(new Runnable() {
    
                @Override
                public void run() {
    
                    try {
                        int port = 41017;
                        InetAddress addr = InetAddress.getByName("10.1.31.156");
                        int i = 1;
                        while (true) {
                            // TODO Auto-generated method stub
                            String sendStr = "Hello ! I'm Client";
                            sendStr = sendStr + " " + i;
                            byte[] sendBuf;
                            sendBuf = sendStr.getBytes();
                            DatagramPacket sendPacket = new DatagramPacket(sendBuf,
                                    sendBuf.length, addr, port);
                            client.send(sendPacket);
                            i++;
                            Thread.sleep(1000);
                        }
                    }
                    catch (SocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (UnknownHostException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
    
                @Override
                public void run() {
    
                    // TODO Auto-generated method stub
                    try {
                        byte[] recvBuf = new byte[1000];
                        DatagramPacket recvPacket = new DatagramPacket(recvBuf,
                                recvBuf.length);
                        while (true) {
                            client.receive(recvPacket);
                            String recvStr = new String(recvPacket.getData(), 0,
                                    recvPacket.getLength());
                            System.out.println("收到:" + recvStr);
                            Thread.sleep(1000);
                        }
                    }
                    catch (SocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }).start();
    
        }
    }
  • 相关阅读:
    k8s
    vue-element-admin实现模板打印
    MAC系统命令
    思维导图工具
    前端如何渲染几万条数据不卡住页面?
    Echar使用案例
    .Net 相关报错解决方案
    Windows 操作技巧
    Docker&K8s
    微信网页授权原理分析
  • 原文地址:https://www.cnblogs.com/diyishijian/p/5124400.html
Copyright © 2011-2022 走看看