Java Network Programming (1) Network Basics [Communication Protocol HTTP|WebSocket|TCP/IP|UDP]

Overview

Computer network refers to connecting multiple computers and their external devices with independent functions in different geographical locations through communication lines to realize resource sharing and information under the management and coordination of network operating systems, network management software and network communication protocols. transfer

Network classification

  • Local Area Network (LAN)
    • A local area network is a network composed of multiple computers used in a small area. The coverage is usually limited to 10 kilometers and belongs to a small area established by a company or department.
  • Metropolitan Area Network (MAN)
    • A metropolitan area network is a network whose scope is between a wide area network and a local area network. Its network coverage can usually extend to the entire city. With the help of communication optical fibers, multiple local area networks are connected to the public city network to form a large network, so that not only the resources within the local area network can be shared, Resources between LANs can also be shared
  • Wide Area Network (WAN)
    • The Guangcheng network is a long-distance network that involves long-distance communications and can cover multiple countries or even the entire world. Because the geographical distance of WAN is too long, information attenuation is very serious. This kind of network generally requires leased dedicated lines, which are connected through interface information processing protocols and lines to form a mesh structure to solve the routing problem.

Network communication protocol

To achieve communication in a computer network, there must be some conventions, namely communication protocols. Including standards for rate, transmission code, code structure, transmission control steps, error control, etc. Common network communication protocols include: TCP/IP protocol , IPX/SPX protocol, etc. In order to enable dialogue between two nodes, a communication tool (ie, port) must be established between them to enable information exchange between them.

TCP/IP protocol: Transmission Control Protocol/Internet Protocol (Transmission Control Protocol/Internet Protocol), which is the most basic and widespread protocol of the Internet. It defines standards for how computers are connected to the Internet and how data is transferred between them. It contains a series of protocols for processing data communication internally, and adopts a 4-layer hierarchical model. Each layer calls the protocol provided by the next layer to complete its own needs.

  • Application layer: Mainly responsible for application protocols, such as HTTP protocol, FTP protocol, etc.
  • Transport layer: It mainly enables network programs to communicate. When communicating over the network, TCP protocol or UDP protocol can be used.
  • Network layer: The network layer is the core of the entire TCP/IP protocol. It is mainly used to group the transmitted data and send the grouped data to the target computer or network.
  • Data link layer: The link layer is used to define the physical transmission channel, usually a driver protocol for certain network connection devices, such as drivers provided for optical fibers and network cables.

HTTP

Overview

HTTP (Hyper Text Transformer Protocol) is a communication protocol. It is an application protocol based on the TCP/IP communication protocol for transmitting HTML and image files. Its main purpose is to define the client ( Browser) and the server to achieve the transmission and access of various resources (such as HTML pages, images, audio, videos, etc.) ;

  • HTTP protocol characteristics
    • Based on request response mode
      • The HTTP protocol adopts a client-server architecture model. The client sends a request to the server, and the server returns a corresponding response. This mode can effectively separate application logic and improve the maintainability and scalability of the system.
    • no connection
      • The HTTP protocol is a connectionless protocol, each request is independent, and the server closes the connection immediately after processing the request. This can save resources, but it also brings some disadvantages, such as the need to re-establish the connection, repeatedly sending the same header information, etc.
    • Transmission efficiency | High reliability
      • Stateless : No history or status information is saved during data transmission.
      • Using TCP as the transport layer protocol
    • Support multimedia transmission
      • The HTTP protocol can transmit various types of data, such as HTML, XML, JSON, pictures, audio, video, etc. This makes the HTTP protocol a universal network transmission protocol suitable for various types of application scenarios.
    • Good compatibility
      • Support B/S, C/S mode
  • Insufficient HTTP protocol
    • The HTTP protocol is transmitted in clear text and can easily be eavesdropped, tampered with or forged.
    • In the request/response model, each request requires establishing a new TCP connection, which increases network communication overhead.
    • The HTTP protocol can only perform simple identity authentication, such as basic authentication and digest authentication. This authentication method is vulnerable to attacks and cannot provide sufficient security.

HTTP request response process

HTTP protocol request response process

HTTP message

request message

 An HTTP request message consists of four parts : request line, request header, blank line, and request data.

Request header

response message

 The response message also consists of four parts: status line, message header, blank line, and response body.

response header

HTTP version

HTTP1.0

HTTP/1.0 was the first HTTP protocol version to specify a version number in communications, and is still widely used in proxy servers. The server does not track each client nor record past requests (stateless). This statelessness can use the cookie/session mechanism for identity authentication and status recording. At the same time, it uses short connections by default, that is, HTTP1.0 provides browsing The browser maintains a short connection with the server. Each request of the browser needs to establish a TCP connection with the server. The TCP connection is immediately disconnected (no connection) after the server completes processing.

The defects are as follows

  • No connection: The connection cannot be reused, and the TCP connection must be re-created for each request, which increases the cost of network communication.
  • Head-of-line blocking: 1.0 stipulates that the next request must be sent after the response to the previous request arrives, otherwise it must be in a waiting state
HTTP1.1
  • Long connection : HTTP1.1 adds a Connection field. By setting Keep-Alive, the HTTP connection can be kept open, avoiding the need to repeatedly establish, release and establish a TCP connection every time the client and server request, improving network utilization. If the client wants to close the HTTP connection, it can carry Connection: false in the request header to tell the server to close the request.
  • Support request pipeline (pipelining): Based on HTTP1.1 long connection, request pipeline is possible. That is, multiple requests can be sent simultaneously on the same TCP connection without waiting for the previous response to return, thus further improving network transmission efficiency.
HTTP2.0
  • Multiplexing: Allows multiple request-response messages to be initiated simultaneously through a single HTTP/2 connection
  • Binary framing: HTTP/2 adds a binary framing layer between the application layer (HTTP/2) and the transport layer (TCP or UDP). Without changing the semantics, methods, status codes, URIs and header fields of HTTP/1.x, it solves the performance limitations of HTTP1.1, improves transmission performance, and achieves low latency and high throughput.

HTTP status codes ( common )

  • 200 OK
  • 302 Found means a temporary redirect, not a permanent redirect
  • 401 Unauthorized indicates that the current request requires user verification.
  • 403 Forbidden means the server has understood the request but refused to execute it
  • 404 Not Found means that the request failed and the resource requested was not found on the server.

  • 500 Internal Server Error means that the processing of the request cannot be completed, usually an internal error on the server side.

HTTP protocol security

The HTTP protocol is transmitted in clear text, that is, all requests and responses are transmitted in clear text . This means it can be eavesdropped, tampered with or forged during transmission. In order to solve the security problems of HTTP protocol, HTTPS protocol came into being. The HTTPS protocol adds the SSL/TLS protocol to the HTTP protocol to ensure data security through digital certificates ( encryption and authentication technologies, including symmetric encryption and decryption, asymmetric encryption and decryption, digital signatures, etc. ) . When using the HTTPS protocol, all communications between the client and server are encrypted and cannot be eavesdropped or tampered with by third parties. In addition, HTTPS also supports certificate authentication, which can verify the identity of the server and prevent forgery and man-in-the-middle attacks.

HTTPS verification process to ensure data transmission security

WebSocket

concept

The WebSocket protocol is a network protocol that relies on the HTTP protocol and is based on the TCP protocol . It realizes full-duplex communication between the browser and the server, that is, while the client sends information to the server, it also allows the server to actively send information to the client.

Features are as follows

  • Real-time communication protocol that allows two-way communication
  • Built on the HTTP protocol, a persistent connection is established through the handshake protocol
  • Use binary protocol to transmit data faster.
  • Support cross-domain communication.
  • Support server push data

Reasons for the WebSocket protocol

Before the emergence of the WebSocket protocol, duplex communication between the client and the client was achieved through HTTP polling ( polling technology generally uses setInerval or setTimeout on browsers ), that is, the client continuously sends different HTTP requests to the host. to make inquiries, thus wasting bandwidth resources ( after all, the original purpose of HTTP was not for two-way communication ). Moreover, sending HTTP requests can only be initiated by the client and cannot be initiated by the server to the client. In order to achieve two-way communication between the client and the server, the WebSocket protocol was born in 2008 after years of development.

working principle

Client and server workflow based on HTTP protocol and WebSocket protocol

The working principle of WebSocket can be divided into three stages: handshake, data transfer and disconnection

  • Handshake
    • When the client initiates a WebSocket connection, the WebSocket protocol reuses the HTTP handshake request process and establishes the connection by sending a special HTTP request header to the server. After the server checks specific fields in the request header to confirm that it supports the WebSocket protocol, it sends a special HTTP response header for handshake confirmation. After the handshake is successful, the two parties establish a WebSocket connection and can perform subsequent data transmission.
  • data transmission
    • After the WebSocket connection is successfully established, the client and server can perform two-way real-time data transmission through the connection. Both parties can send and receive messages, and messages are transmitted in the form of frames . The WebSocket protocol defines different types of frames, such as text frames and binary frames, for transmitting different types of data
  • Disconnect
    • When a connection is no longer needed, the client or server can initiate a request to close the connection. Both parties will exchange special close frames to negotiate the closure of the connection and ensure that both parties receive the close request.

WebSocket Application

WebSocket API ( front-end example )
  • WebSocket API is an interface that uses the WebSocket protocol to establish a full-duplex channel to send and receive messages. During the initial handshake between the client and the server, the http protocol is upgraded to the WebSocket protocol and a connection is established. The underlying layer is TCP protocol. . Once the connection is established, messages can be sent repeatedly through the WebSocket interface
  • WebSocket API is purely event-driven. Once a full-duplex connection is established, when the server sends data or resources to the client, it can automatically send data and notifications of status changes.
  • Commonly used WebSocket events: onopen, onmessage, onclose, etc.
  • Commonly used WebSocket methods: send() and close()
  • WebSocket属性:readyState(open、connecting、closing、closed)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Websocket</title>
</head>
<body>
<script>
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:9999/hello");
        //相当于channelRead0,读取服务器端的消息
        socket.onmessage = function(ev){
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;
        }
        //开启连接
        socket.onopen = function(ev){
            var rt = document.getElementById("responseText");
            rt.value = "开启连接成功!";
        }
        //连接关闭
        socket.onclose = function(ev){
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + "连接关闭成功!";
        }
    }
    //发送消息给服务器
    function send(msg){
        if(!window.socket){ //是否已创建socket
            return;
        }
        if(socket.readyState == WebSocket.OPEN){
            socket.send(msg);
        }else{
            alert("socket未连接");
        }
    }
</script>
<form onsubmit="return false">
    <textarea name="message" style="height:300px;width:300px"></textarea>
    <input type="button" value="Send" onclick="send(this.form.message.value)">
    <textarea id="responseText" style="height:300px;width:300px"></textarea>
    <input type="button" value="Clear" onclick="document.getElementById('responseText').value=''">
</form>
</body>
</html>

TCP

Transmission Control Protocol (Transmission Control Protocol), TCP protocol is a connection-oriented communication protocol, that is, before transmitting data, the sender and the receiver establish a logical connection, and then transmit the data. It provides reliable and error-free data between two computers. Transmission; in the TCP connection, the client and the server must be clearly defined. The client sends a connection request to the server. Each connection creation requires a "three-way handshake". When the TCP client and server disconnect, wave four times

three handshakes

TCP three-way handshake
  • The first handshake: the client sends a SYN (SEQ=x) message to the server and enters the SYN_SEND state. (The client sends a connection request to the server and waits for confirmation from the server)
  • Second handshake: The server receives the SYN message, responds with a SYN (SEQ=y) ACK (ACK=x+1) message, and enters the SYN_RECV state (the server sends a response back to the client to notify the client that it has received connection request)
  • The third handshake: the client receives the SYN message from the server, responds with an ACK (ACK=y+1) message, and enters the Established state (the client sends confirmation information to the server again to confirm the connection)

The three-way handshake ends, the TCP client and server successfully establish a connection, and then data can be transmitted~

wave four times

  • The client intends to close the connection. At this time, it will send a message with the FIN flag bit in the TCP header set to 1, that is, a FIN message. After that, the client enters the FIN_WAIT_1 state.
  • After receiving the message, the server sends an ACK response message to the client, and then the server enters the CLOSED_WAIT state.
  • After receiving the ACK response message from the server, the client enters the FIN_WAIT_2 state.
  • After waiting for the server to process the data, it also sends a FIN message to the client, and then the server enters the LAST_ACK state.
  • After receiving the FIN message from the server, the client returns an ACK response message and then enters the TIME_WAIT state.
  • After the server receives the ACK response message, it enters the CLOSE state. At this point, the server has completed closing the connection.
  • After a period of 2MSL, the client automatically enters the CLOSE state. At this point, the client has also completed closing the connection.

As can be seen from the above figure, there must be a FIN and an ACK in each direction, so it is usually called four waves. Note on this point: Only when you actively close the connection can you enter the TIME_WAIT state.

UDP

  • User Datagram Protocol (Datagram is the basic unit of UDP network transmission)
  • UDP is a connectionless communication protocol, that is, the sender and receiver of data do not establish a logical connection during data transmission. That is, when one computer sends data to another computer, the sending end will not confirm whether the receiving end exists before sending the data. Similarly, when the receiving end receives the data, it will not feedback to the sending end whether the data has been received.
  • Since the UDP protocol consumes less resources and has high communication efficiency, it is usually used for the transmission of audio, video and ordinary data. For example, video conferencing uses the UDP protocol, because in this case even if one or two data packets are occasionally lost, it will not cause any damage. Too much impact on reception results.
  • However, when using the UDP protocol to transmit data, due to the connectionless nature of UDP, the integrity of the data cannot be guaranteed. Therefore, it is not recommended to use the UDP protocol when transmitting important data.

IP protocol

concept

IP Protocol (Internet Protocol), also known as Internet Protocol, is a data packet protocol that supports interconnection between networks. This protocol works at the network layer, and its main purpose is to improve the scalability of the network. Compared with the transport layer TCP, the IP protocol provides a connectionless/unreliable, best-effort data packet transmission service, which is similar to the TCP protocol ( Transmission Control Protocol) together form the core of the TCP/IP protocol suite

Classification
  • IPv4: Assign a 32bit address to each host connected to the network. According to TCP/IP regulations, IP addresses are represented in binary and each IP address is 32 bits long, that is, 4 bytes; for ease of use, IP addresses use dotted decimal notation, such as "192.168.1.66"
  • IPv6: Due to the booming development of the Internet, the demand for IP addresses is increasing. In order to expand the address space, 128-bit address length is used to define addresses through IPv6, which solves the problem of insufficient network address resources.

  • 常用命令:
    ① ipconfig:查看本机 IP 地址(Linux下对应ifconfig)
    ② ping IP 地址:检查网络是否连通。

other

The difference between websocket && HTTP protocol

Same point

Both are application layer protocols used to transmit data between clients and servers.

difference

  • There are different ways to establish a connection
    • In HTTP, the connection needs to be re-established with every request. When a web page needs to continuously send requests to the server, the connection needs to be re-established for each request, which results in connection overhead and network latency.
    • In WebSocket, a connection only needs to be established once between the client and the server, and then the connection can be maintained and two-way communication can be carried out.
  • Data transmission methods are different
    • In HTTP, data is transmitted through the request-response model. The client sends a request -> the server returns a response. The communication between the client and the server in this process is through text.
    • In WebSocket, the communication between client and server is in binary form. This means that WebSocket can transfer data faster and be able to handle more complex data types
  • Supported data types are different
    • In HTTP, the supported data types are limited. Typically only static data types such as text, images, and audio are supported
    • In WebSocket, since data transmission is in binary form, it can support more complex data types, such as video streams and real-time game data.
  • Security is different
    • In HTTP, data is transmitted in clear text, so it is easy to be stolen or tampered with. To solve this problem HTTPS was introduced
    • In WebSocket, data is transmitted in binary form, so it is more secure and cannot be easily stolen or tampered with.
  • Different real-time
    • Due to the request-response model of HTTP, the client can only update data after receiving a response from the server. That is to say, in applications with high real-time requirements, HTTP may not be able to meet the requirements.
    • WebSocket can achieve real-time two-way communication, so it is more suitable for applications with high real-time requirements.

Guess you like

Origin blog.csdn.net/qq_34020761/article/details/132332912