• Java网络编程


    1.网络编程概述

    1.1 网络编程概述

      1. TCP(Transmission Control Protocol):传输控制协议是一种面向连接的、可靠的、基于字节流的传输层通信协议。
      • 例子: 打电话 —> 连接 ----> 接了 -------> 通话,就是TCP
      1. UDP(User Datagram Protocol)用户数据报协议:一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务。
      • 例子:发短信 ------> 发送了就完事了 ------> 接收, 就是UDP。

    1.2 计算机网络

    计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路和通信设备连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。(来源百度百科)

    1.3 网络编程目的

    无线电台…
    传播交流信息
    数据交换
    通信

    1.4 想要达到这个效果需要什么

    1.如何准确的定位一台主机 192.168.x.x : 端口,定位到这个计算机的某个资源
    2. 找到了主机如何传输数据呢?

    2.网络通信的要素

    2.1 如何实现网络通信

      1. 通信双方的地址:
      • ip
      • 端口号
      • 192.168.x.x:9999
      1. 规则:网络通信协议
      • http
      • ftp
      • smtp
      • udp
      • tcp
      1. TCP/IP 参考模型
        在这里插入图片描述

    小结

    1. 网络编程中有两个主要的问题
      1.1 如何准确的定位到网络上的一台或多台主机
      1.2 找到主机后如何进行通信
    2. 网络编程中的要素
      1.1 IP和端口号
      1.2 网络通信协议: UDP和TCP(目前要学的)
    3. 万物皆对象

    3. IP

      1. ip地址:在net包下面,InetAddress类
      • 唯一定位一台网络上的计算机
      • 127.0.0.1: 本机localhost
      • ip地址的分类:
        • ipv4 / ipv6
          • IPV4 127.0.0.1,由四个字节组成,0-255,42亿(北美30亿),亚洲4亿 ,2011年就用完了
          • IPV6 fe80::1f12:2c22:c818:7a59%5,128位(每一位都是16进制,共8个位置16*8)。8个无符号整数
        • 公网(互联网)-私网 (局域网)
          • ABCD类地址
          • 192.168.x.x,专门给组织内部使用的
      • 域名:记忆IP问题
        • IP:www.baidu.com

    3.1 InetAddress

      1. 不能直接创建对象:构造器的修饰符为默认的default,作用域为当前包,所以这个类不能创建对象。
        在这里插入图片描述
      1. 如何使用:
    package com.IP;
    
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.Arrays;
    
    /**
     * @Description
     * 测试IP
     * @Author zhaopeng
     * @Date 2023/10/12 10:49
     */
    public class TestInetAddress {
        public static void main(String[] args)  {
    
            try {
                // 查询本机和百度的地址
                InetAddress baiduName = InetAddress.getByName("www.baidu.com");
                InetAddress localhost1 = InetAddress.getByName("localhost");
                InetAddress localHost = InetAddress.getLocalHost();
    
                System.out.println("baiduName-"+baiduName);
                System.out.println("localhost1-"+localhost1);
                System.out.println("localHost-"+localHost);
    
    
                // 常用的方法
                System.out.println("baiduName.getAddress()--" + Arrays.toString(baiduName.getAddress()));;
                System.out.println("baiduName.getCanonicalHostName()--"+baiduName.getCanonicalHostName()); // 规范的名字
                System.out.println("baiduName.getHostAddress()--"+baiduName.getHostAddress()); // 获取主机名地址(ip)
                System.out.println("baiduName.getHostName() -- "+baiduName.getHostName());// 获取域名,或者自己电脑的名字
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    4. 端口

      1. 端口表示计算机上的一个程序的进程;
      • 不同的进程有不同的端口号,用来区分软件
      • 被规定0~65535
      • TCP,UDP :65535 * 2,这俩协议可以同时使用同一个端口,不会冲突,但是同一个协议不能同时使用同一个端口。
      1. 端口分类:
      • 公用端口:0~1023
        • HTTP:80
        • HTTPS:443
        • FTP:21
        • SSH:22
        • Telent:23
      • 程序注册端口:1024~49151,分配给用户或者程序
        • Tomcat:8080
        • MySQL:3306
        • Oracle:1521
      • 动态、私有:49152~65535
      • dos命令:
        netstat -ano #查看所有的端口
        netstat -ano|findstr "5900" # 查看指定的端口
        tasklist|findstr "8696" 查看指定的端口的进程
        
        • 1
        • 2
        • 3
      1. 使用InetSocketAddress
    package com.IP;
    
    import java.net.InetSocketAddress;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 11:25
     */
    public class TestInetSocketAddress {
        public static void main(String[] args) {
            InetSocketAddress address = new InetSocketAddress("www.baidu.com", 80);
            InetSocketAddress localhost = new InetSocketAddress("localhost", 8080);
    
            System.out.println(address);
            System.out.println(localhost);
    
            System.out.println(address.getHostName()); // 获取地址
            System.out.println(address.getAddress());
            System.out.println(address.getPort());// 获取端口
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    5. 通信协议

    • 协议:约定,好比我们现在说的普通话。
    • 网咯通信协议:速率,传输码率,代码结构,传输控制…
    • TCP/IP协议簇(一组协议)
      • 重要
        * TCP:用户传输协议
        * UDP:用户数据报协议
    • TCP和UDP对比
      • TCP:打电话
        • 连接,稳定
        • “三次握手”,”四次挥手“
        • 客户端,服务端
        • 传输完成,释放连接,效率低
      • UDP:发短信
        • 不连接,不稳定
        • 客户端,服务端:没有明确的界限
        • 不管有没有准备好接受,我都会发给你
        • DDOS:洪水攻击!(饱和攻击)

    6.实现客户端,服务器通信简单案例

    6.1 服务端

    • 步骤:
        1. 建立服务端口ServerSocket
        1. 等待用户的连接ServerSocket.accept 。返回的是Socket对象
        1. 接受用户的消息
    package com.IP.Demo;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * @Description
     *  服务器
     * @Author zhaopeng
     * @Date 2023/10/12 13:38
     */
    public class TcpServer {
        public static void main(String[] args) {
            ServerSocket serverSocket =null;
            Socket socket = null;
            InputStream is = null;
            ByteArrayOutputStream baos = null;
            try {
                //        1.创建端口号
                serverSocket = new ServerSocket(9999);
                while (true){
                    //  2. 等待客户端连接
                   socket = serverSocket.accept();
                   // 3. 读取客户端的消息
                    is = socket.getInputStream();
    
                    // 来个管道流方便服务器接受客户端信息
                    baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = is.read(buffer)) != -1){
                        baos.write(buffer,0,len);
                    }
                    System.out.println(baos.toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (baos!=null){
                    try {
                        baos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (serverSocket != null){
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    6.2 客户端

    • 步骤:
      • 1.连接服务器Socket(需要知道服务器的ip和port)
      • 2.发送消息
    package com.IP.Demo;
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    /**
     * @Description 客户端
     * @Author zhaopeng
     * @Date 2023/10/12 13:38
     */
    public class TcpClient {
        public static void main(String[] args) {
            OutputStream os = null;
            Socket socket = null;
            try {
                // 1.需要知道服务器的地址,端口号
                InetAddress serverIp = InetAddress.getByName("localhost");
                int port = 9999;
    //            2.创建一个socket连接
                socket = new Socket(serverIp,port);
                // 3. 发送消息  IO流
                os = socket.getOutputStream();
                os.write("你好,我是客户端,我要写东西".getBytes());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (os != null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if (socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    7. 实现文件上传,客户端服务端案例

    • 客户端
    package com.IP.FileDemo;
    
    import java.io.*;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    /**
     * @Description
     * 文件上传
     * @Author zhaopeng
     * @Date 2023/10/12 14:33
     */
    public class FileClient {
        public static void main(String[] args) throws Exception {
            // 1.创建一个Socket连接
            Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
            // 2. 创建一个 输出流
            OutputStream os = socket.getOutputStream();
            // 3. 读取文件
            FileInputStream is = new FileInputStream(new File("2.jpg"));
            // 4. 写出文件
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
            // 通知服务器已经结束
            socket.shutdownOutput();
    
            // 确定服务器接收完毕,再断开
            InputStream is2 = socket.getInputStream();
            // 来个管道,把is2接收到的东西转化一下
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer2 = new byte[1024];
            int len2;
            while ((len2 = is2.read(buffer2)) != -1){
                baos.write(buffer2,0,len2);
            }
            System.out.println(baos.toString());
            // 关闭资源
            baos.close();
            is2.close();
            is.close();
            os.close();
            socket.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 服务端
    package com.IP.FileDemo;
    
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 14:37
     */
    public class FileServer {
        public static void main(String[] args) throws Exception{
            // 1.创建serversocket服务端口
            ServerSocket serverSocket = new ServerSocket(9000);
            // 2. 监听客户端的连接,等待连接
            Socket accept = serverSocket.accept(); // 阻塞式监听,会一直等待
    //        3. 获取输入流
            InputStream is = accept.getInputStream();
    //        4.文件输出
            // 需要一个管道来接受
            FileOutputStream fos = new FileOutputStream(new File("testtest.jpg"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
            // 传输完毕开始向客户端发送消息
            OutputStream outputStream = accept.getOutputStream();
            outputStream.write("已经接受完毕,情断开".getBytes());
            // 5.关闭资源
            outputStream.close();
            fos.close();
            is.close();
            accept.close();
            serverSocket.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    8. 实现UDP通信

    • 发送端Demo
    package com.IP.UDPDemo;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 15:33
     */
    //不需要连接服务器
    public class UDPClient {
        public static void main(String[] args) throws Exception{
            //        1. 建立一个Socket
            DatagramSocket socket = new DatagramSocket();
            //        2. 建立一个包
            String msg = "这里是UDP协议通讯";
            InetAddress localhost = InetAddress.getByName("localhost");
            int port = 9000;
            DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
    //        3. 发送包
            socket.send(packet);
    //        4.关闭流
            socket.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 接收端Demo
    package com.IP.UDPDemo;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 15:33
     */
    public class UDPServer {
        public static void main(String[] args) throws Exception {
            // 开放端口
            DatagramSocket socket = new DatagramSocket(9000);
            // 开始接受数据包
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
    
            socket.receive(packet); // 阻塞式接受
    
            System.out.println(packet.getAddress().getHostAddress()); // 获取ip
            System.out.println(new String(packet.getData(),0,packet.getLength()));
    
            // 关闭连接
            socket.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    9. 实现聊天

    · 发送方:

    package com.IP.UDPDemo.chat;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 15:54
     */
    public class UdpSender {
        public static void main(String[] args) throws Exception {
            DatagramSocket socket = new DatagramSocket(8888);
    
            // 从控制台获取数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                String data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
                // 发送
                socket.send(packet);
                if ("bye".equals(data)){
                    break;
                }
            }
            socket.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 接收方
    package com.IP.UDPDemo.chat;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.util.Arrays;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 15:53
     */
    public class UdpReceive {
        public static void main(String[] args) throws Exception{
           DatagramSocket socket = new DatagramSocket(6666);
    
           while (true){
               // 准备接受包裹
               byte[] buffer= new byte[1024];
               DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
               socket.receive(packet); // 阻塞式接受
    //        断开连接
               byte[] data = packet.getData();
               String receiveData = new String(data, 0, data.length);
               System.out.println(receiveData);
               if ("bye".equals(receiveData)){
                   break;
               }
           }
           socket.close();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    10 在线咨询(多线程)

    TalkSender实现了Runnable接口

    package com.IP.UDPDemo.chat;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 16:24
     */
    public class TalkSender implements Runnable{
        DatagramSocket socket = null;
        BufferedReader reader = null;
    
        private int fromPort;
        private String toIP;
        private int toPort;
    
        public TalkSender(int fromPort, String toIP, int toPort) {
            this.fromPort = fromPort;
            this.toIP = toIP;
            this.toPort = toPort;
    
            try {
                socket =   new DatagramSocket(fromPort);
                // 从控制台获取数据
                reader = new BufferedReader(new InputStreamReader(System.in));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            while (true) {
                try {
                    String data = reader.readLine();
                    byte[] datas = data.getBytes();
                    DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(toIP, toPort));
                    // 发送
                    socket.send(packet);
                    if ("bye".equals(data)){
                        break;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            socket.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • TalkReceive(实现了Runnable接口)
    package com.IP.UDPDemo.chat;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 16:28
     */
    public class TalkReceive implements Runnable{
    
    
        private String msgFrom;
        private int fromPort;
        DatagramSocket socket = null;
    
        public TalkReceive(int fromPort,String msgFrom) {
            this.fromPort = fromPort;
            this.msgFrom =msgFrom;
            try {
                socket = new DatagramSocket(this.fromPort);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            while (true){
                try {
                    // 准备接受包裹
                    byte[] buffer= new byte[1024];
                    DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
                    socket.receive(packet); // 阻塞式接受
    //        断开连接
                    byte[] data = packet.getData();
                    String receiveData = new String(data, 0, data.length);
                    System.out.println(msgFrom + ":"+receiveData);
                    if ("bye".equals(receiveData)){
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 学生类:
    package com.IP.UDPDemo.chat;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 16:35
     */
    public class Student {
        public static void main(String[] args) {
            new Thread(new TalkSender(7778,"localhost",9998)).start();
            new Thread(new TalkReceive(6666,"老师")).start();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 老师类:
    package com.IP.UDPDemo.chat;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 16:37
     */
    public class Teacher {
        public static void main(String[] args) {
            new Thread(new TalkSender(4444,"localhost",6666)).start();
            new Thread(new TalkReceive(9998,"学生")).start();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    11 URL下载网络资源

    URL:统一资源定位符,定位互联网上的某一个资源
    DNS:域名解析 www.baidu.com x.xx.x.x 把域名解析为ip地址
    
    • 1
    • 2

    1. 协议://ip地址:端口/项目名/资源
    
    • 1
    • URL里面都有什么
    package com.IP.URL;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 17:30
     */
    public class URLTest {
        public static void main(String[] args) throws IOException {
            URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=dapeng&password=123");
            System.out.println("协议:" + url.getProtocol());
            System.out.println("主机IP:" + url.getHost());
            System.out.println("端口:" + url.getPort());
            System.out.println("文件路径:" + url.getPath());
            System.out.println("全路径:" + url.getFile());
            System.out.println("参数:" + url.getQuery());
            System.out.println("主机IP+端口:" + url.getAuthority());
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 效果:
      在这里插入图片描述
    • 下载网络某文件:
    
    package com.IP.URL;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    /**
     * @Description
     * @Author zhaopeng
     * @Date 2023/10/12 17:40
     */
    public class DownLoadFile {
        public static void main(String[] args) throws Exception {
            URL url = null;
            // 1. 下载地址
    
            url = new URL("https://m701.music.126.net/20231012181404/f7d562aebfe48ec3e14d1582c678bf9d/jdyyaac/060c/0e5c/5259/1f601e5eb2966119ebfde37f17c0bb41.m4a");
    
            // 2. 开启连接,HttpURLConnection
    
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 3. 获取文件
            InputStream is = connection.getInputStream();
            // 4.下载文件
            FileOutputStream fos = new FileOutputStream("f.m4a");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer);
            }
    
            fos.close();
            is.close();
            connection.disconnect();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    KubeSphere介绍和基于K8S的安装
    THREEJS基础入门
    基于蒙特卡诺的风场景模型出力(Matlab代码实现)
    猿创征文|Spring MVC学习大总结
    配置文件和yaml语法
    pod 镜像拉取策略
    2024最新免费的mac电脑清理垃圾的软件有哪些?
    C++ 实现HTTP的客户端、服务端demo和HTTP三方库介绍
    错误的方向,加上自学就相当于是在自己慢性自杀。
    Alkyne-PEG-COOH 炔烃PEG羧基
  • 原文地址:https://blog.csdn.net/fghjhdrf/article/details/133783936