• 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();
    
        }
    }
  • 相关阅读:
    数据库的接口
    BionicThe README from the bionic/libc
    发现问题——创新的原动力
    使用ADO或ADO控件访问数据库
    游标、事务并发和锁三者之间的那点事
    处女座——菜鸟程序员的工程总结
    数据库的基础知识以及创建数据库
    《Team Geek》前言(中文,自己翻译的)
    万里长征,始于足下——菜鸟程序员的学习总结(一)
    与RMAN相关的动态性能视图
  • 原文地址:https://www.cnblogs.com/diyishijian/p/5124400.html
Copyright © 2020-2023  润新知