• Java学习笔记52(网络编程:UDP协议案例)


    InetAddress类:

    表示互联网中的IP地址,示例:

    package demo;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class InetAddressDemo {
        public static void main(String[] args) throws UnknownHostException {
            function1();
            function2();
        }
    
        public static void function1() throws UnknownHostException {
            InetAddress inet = InetAddress.getLocalHost();
            System.out.println(inet.getHostName());// 获取主机名
            // 输出:DESKTOP-Q3O8AEO
            System.out.println(inet.getHostAddress());// 获取主机IP
            // 输出:192.168.87.1
        }
    
        public static void function2() throws UnknownHostException {
            // 获得其他IP
            InetAddress inet = InetAddress.getByName("www.baidu.com");
            System.out.println(inet);
            // 输出:www.baidu.com/111.13.100.92
        }
    }
    View Code

    UDP协议:无连接通信协议,最大64KB,不安全

    TCP协议:面向连接通信协议,可靠安全,三次握手以确认连接,相对速度慢,适合大数据传输

    UDP示例:

    发送端:

    package demo;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    public class UDPSend {
        public static void main(String[] args) {
            try {
                byte[] data = "你好".getBytes();
                InetAddress inet = InetAddress.getByName("127.0.0.1");
                DatagramPacket dp = new DatagramPacket(data, data.length, inet, 6000);
                DatagramSocket ds = new DatagramSocket();
                ds.send(dp);
                ds.close();
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    }
    View Code

    接收端:

    package demo;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    public class UDPReceive {
        public static void main(String[] args) {
            try {
                DatagramSocket ds = new DatagramSocket(6000);
                byte[] data = new byte[1024];
                DatagramPacket dp = new DatagramPacket(data, data.length);
                ds.receive(dp);
                int length = dp.getLength();
                String ip = dp.getAddress().getHostAddress();
                System.out.println(ip + ":" + new String(data, 0, length));
                ds.close();
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    }
    View Code

    先运行接收端,再运行发送端,结果:

    输出:127.0.0.1:你好

    功能可以提升,实现键盘输入的聊天:

    发送端:

    package demo;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.util.Scanner;
    
    public class UDPSend {
        public static void main(String[] args) {
            try {
                Scanner sc = new Scanner(System.in);
                DatagramSocket ds = new DatagramSocket();
                InetAddress inet = InetAddress.getByName("127.0.0.1");
                while (true) {
                    String message = sc.nextLine();
                    byte[] data = message.getBytes();
                    DatagramPacket dp = new DatagramPacket(data, data.length, inet, 7000);
                    ds.send(dp);
                }
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    }
    View Code

    接收端:

    package demo;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    public class UDPReceive {
        public static void main(String[] args) {
            try {
                DatagramSocket ds = new DatagramSocket(7000);
                byte[] data = new byte[1024];
                while (true) {
                    DatagramPacket dp = new DatagramPacket(data, data.length);
                    ds.receive(dp);
                    int length = dp.getLength();
                    String ip = dp.getAddress().getHostAddress();
                    System.out.println(ip + ":" + new String(data, 0, length));
                }
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    }
    View Code

    先后运行,在发送端发送的消息可以随时在接收端收到,这里就实现了一个简单的聊天功能

    同个局域网下的多台电脑之间也可以聊天,只要修改成相应的IP地址即可

  • 相关阅读:
    C语言实现五子棋简单功能
    C语言学习笔记——堆和栈——未整理
    《C和指针》章节后编程练习解答参考——第10章
    华为06年面试题——求交换数组元素后差值最小方案
    丢沙包游戏(或杀人游戏)的C语言实现
    《C和指针》章节后编程练习解答参考——第9章
    PHP函数:array_key_exists
    PHP函数:php_sapi_name
    PHP函数:json_last_error
    PHP函数:func_num_args
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/8334646.html
Copyright © 2020-2023  润新知