• java开发:网络编程


    记录Java网络编程相关知识,欢迎大家沟通交流

    一、IP

    Ip地址:InetAddress类

    唯一定义一台网络上的计算机。
    127.0.0.1:本机localhost

    ip地址的分类:

    1、ipv4/ipv6

    • ipv4 127.0.0.1 , 4个字节组成,0~255 ,共42亿个,2001年就用尽了
    • ipv6 128位,8个无符号整数(2001:0bb2:aaaa:0015:0000:0000:1aaa:1213)

    2、公网(互联网)-私网(局域网)

    • ABCD类地址
    • 192.168.xxx.xxx:专给组织内部使用

    域名:

    解决记忆IP问题,方便记忆,最终域名会映射到IP上。

    三个获取IP地址的方法

                InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
                System.out.println(inetAddress1);
                InetAddress inetAddress2 = InetAddress.getByName("localhost");
                System.out.println(inetAddress2);
                InetAddress inetAddress3 = InetAddress.getLocalHost();
                System.out.println(inetAddress3);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    二、端口

    端口表示计算机上一个程序的进程。

    A. 不同的进程有不同的端口号,用来区分软件。

    B. 端口号范围:0~65535

    C. 端口分类

    • 公有端口(系统使用或者是其他协议默认端口)0~1023
      HTTP:80、HTTPS:443、FTP:21、Telent:23
    • 程序注册端口1024~49151 ,分配用户或者程序
      Tomcat:8080、MySQL:3306、Oracle:1521
    • 动态、私有:49152~65535

    三、TCP

    客户端:
    1、连接服务器Socket
    2、发送消息

    服务端:
    1、建立服务端的端口 ServerSocket
    2、等待用户的连接 accept
    3、接收消息

    收发消息示例

    
    
    /**
     * 客户端
     */
    public class TCPClient {
        public static void main(String[] args) {
            Socket socket = null;
            OutputStream outputStream = null;
            try {
                InetAddress localHost = InetAddress.getLocalHost();
                int port = 9999;
                socket = new Socket(localHost, port);
                outputStream = socket.getOutputStream();
                outputStream.write("你好,世界!".getBytes());
                outputStream.flush();
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    
    
    
    /**
     * 服务端
     */
    public class TCPServer {
    
        public static void main(String[] args) {
            ServerSocket serverSocket = null;
            Socket accept = null;
            InputStream inputStream = null;
            ByteArrayOutputStream byteArrayOutputStream = null;
            try {
                serverSocket = new ServerSocket(9999);
    
    
                while (true) {
                    accept = serverSocket.accept();
                    inputStream = accept.getInputStream();
                    byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buffer)) != -1) {
                        byteArrayOutputStream.write(buffer, 0, len);
                    }
                    System.out.println(byteArrayOutputStream.toString());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    byteArrayOutputStream.close();
                    inputStream.close();
                    accept.close();
                    serverSocket.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    
    
    • 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
    • 75

    TCP上传文件流

    在项目的根目录下拷贝client.jpg图片
    
    
    /**
     * 服务器
     */
    public class TCPUploadFileServer {
        public static void main(String[] args) {
            try {
                ServerSocket serverSocket = new ServerSocket(9999);
                Socket accept = serverSocket.accept();
                InputStream inputStream = accept.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(new File("receive.jpg"));
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                fileOutputStream.flush();
    
                OutputStream outputStream = accept.getOutputStream();
                outputStream.write("服务端接收完毕".getBytes());
                outputStream.flush();
    
                outputStream.close();
                fileOutputStream.close();
                inputStream.close();
                accept.close();
                serverSocket.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    /**
     * 客户端
     */
    public class TCPUploadFileClient {
        public static void main(String[] args) {
            try {
                InetAddress host = InetAddress.getByName("127.0.0.1");
                int port = 9999;
                Socket socket = new Socket(host, port);
                OutputStream outputStream = socket.getOutputStream();
                FileInputStream fileInputStream = new FileInputStream(new File("client.jpg"));
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fileInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
                outputStream.flush();
    
                //通知服务器输出流传输完毕
                socket.shutdownOutput();
    
                InputStream inputStream = socket.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] iBuffer = new byte[1024];
                int ilen;
                while ((ilen = inputStream.read(iBuffer)) != -1) {
                    byteArrayOutputStream.write(iBuffer, 0, ilen);
                }
                System.out.println(byteArrayOutputStream.toString());
    
                fileInputStream.close();
                outputStream.close();
                socket.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    • 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

    四、Tomcat

    服务端
    1、自定义(S端)
    2、Tomcat服务器(S端):java后台开发

    客户端
    1、自定义(C端)
    2、浏览器(B端)

    五、UDP

    UDP不存在服务端、客户端的概念,可以互相发送

    public class UDPClient {
        public static void main(String[] args) {
            try {
                DatagramSocket socket = null;
    
                socket = new DatagramSocket();
    
                InetAddress host = InetAddress.getByName("127.0.0.1");
                int port = 9090;
                String msg = "你好啊,服务器";
                DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, host, port);
    
                socket.send(datagramPacket);
    
                socket.close();
    
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    public class UDPServer {
        public static void main(String[] args) {
            {
                try {
                    DatagramSocket socket = null;
    
                    socket = new DatagramSocket(9090);
    
                    byte[] buffer = new byte[1024];
                    DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);
    
                    socket.receive(datagramPacket);
    
                    System.out.println(datagramPacket.getAddress().getHostAddress());
                    System.out.println(new java.lang.String(datagramPacket.getData(), 0, datagramPacket.getLength()));
    
                    socket.close();
    
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    
    • 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

    六、URL

    http://www.baidu.com/
    统一资源定位符:用于定位资源,定位互联网上的某一个资源。
    DNS域名解析:把 域名 解析成 IP+Port
    URL组成结构:协议://IP地址:端口/项目名/资源

    
    /**
     * URL网络编程,常用api示例
     */
    public class URLDemo001 {
        public static void main(String[] args) {
            try {
                URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=xiaowu&password=123");
                System.out.println(url.getProtocol());//协议
                System.out.println(url.getHost());//主机名
                System.out.println(url.getPort());//端口
                System.out.println(url.getPath());//文件相对路径
                System.out.println(url.getFile());//全路径
                System.out.println(url.getQuery());//参数
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    /**
     * URL网络编程,下载网络图片
     */
    public class URLDemo001 {
        public static void main(String[] args) {
            try {
                URL url = new URL("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                //文件会下载到根目录下(项目根目录)
                FileOutputStream fileOutputStream = new FileOutputStream(new File("baidu.png"));
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                fileOutputStream.flush();
    
                fileOutputStream.close();
                inputStream.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    • 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
                                           **end**
    
    • 1
  • 相关阅读:
    【python基础】函数-值传递
    指针进阶(续)
    python字符编码
    rust重载比较运算符
    连连看游戏页面网站源码
    JavaWeb-JSP
    使用JS实现一个简单的观察者模式(Observer)
    华为SRG2200 端口映射 & 双向NAT & 回流
    架构演进,阿里资深Java工程师带你手撕之架构大全(性能优化+分布式+框架+微服务)
    Java基础知识面试总结
  • 原文地址:https://blog.csdn.net/android157/article/details/128047632