TCP, UDP, IP

TCP is a transport layer protocol, UDP is a transport layer protocol, and IP is a network layer protocol.

IP: is used to find the target computer. For example, if A sends data to B, it needs to find the computer of the other party B, and after finding it, it determines the channel between the network, A-B. That is, IP is used to determine the path between networks.

TCP: Used for communication. It is a full-duplex protocol (described below). After IP implements an inter-network path, TCP provides full duplex communication over this path. It takes three handshakes to establish the link and four waves to break it (explained below).

UDP: Similar to TCP, UDP provides communication capabilities. But it’s different from TCP. TCP is point-to-point communication, UDP supports one-to-many, many-to-one, one-to-one, and so on. UDP does not need to link, directly send data, relatively faster, but does not guarantee that the data will arrive, suitable for broadcast.

Full duplex half duplex and simplex

Full-duplex: INDICATES that user A can send data to USER B and user B can also send data to user A.

Half-duplex: When user A sends data to user B, user B cannot send data to user A. When B sends data to A, A cannot send data to B.

Simplex: indicates that either A can send data only to B, or B can send data only to A. Data is sent in one direction, not both.

TCP three-way handshake

TCP requires three handshakes to establish a connection and four waves to disconnect. In the following example, A is the client and B is the server to illustrate the three-way handshake and four-way wave.

Note: TCP is A full-duplex protocol, so SYN=1 is sent twice when establishing A connection. A sends SYN=1 to B when establishing A connection. B also sends SYN=1 to A. Similarly, send FIN=1 twice for each of the four waves.

First handshake: Client A sends SYN=1 (to establish A connection to B), SEq = A (A serial number for sending data) to the server. Second handshake: After server B receives data, it sends ACK=1,SYN=1, SEq = B, and ACK= A +1 to client A.

Third handshake: After receiving the data, client A checks whether the ACK sent last time is seQ +1= A +1. If no problem is found, server B is sent ACK=1, SEq = A +1, and ACK= B +1 (seQ +1, representing the confirmation code when the data is received).

TCP waved four times

First wave: Client A sends FIN=1 to server B (A requests to disconnect the server) and SEQ = A (serial number of the current data).

Second wave: Server B sends ACK=1 to client A. SEq = A +1 indicates that there is still data to send.

Third wave: The server sends FIN=1 (the server wants to disconnect A), SEq = B (the serial number of this data), ACK = A +1 (confirmation code, indicating that no data is to be sent) to client A.

Fourth wave: Client A verifies that the ack received is equal to A +1 and that client B has no more data to send. It then sends to server B, ACK(reply),seq= A +1(sequence number of this data).

Http

HTTP is an application layer protocol, which implements how to send and receive data, defines the format of sending and receiving data, and defines the short connection. Disconnect when a transfer of data ends.

The characteristics of

  1. Only the client sends the request actively and the server responds passively. The server cannot actively send data to the client.
  2. Http1.0 is a short connection that breaks when a link data transfer is complete.
  3. The http1.1 front end and back end can be configured with long links. By default, 60 seconds, when a link is successful, after sending a data, if the front end requests data again, then the last TCP channel will be reused, and the countdown is refreshed. When the client does not request data after 60 seconds, the TCP connection is disconnected. This countdown time is configurable.

request

  1. The request line says whether it’s a GET method, whether it’s a POST method, etc., whether it’s a protocol version HTTP 1.1, etc., so the GET method has a URL. The get method usually requires ASCII characters, i.e. no Chinese characters, and has a length limit. Post does not. In fact, this is usually the browser and other restrictions, not the POST request line can not put parameters.

  2. The request header contains user-Agent: the browser type, the source of the request data. Accept: Specifies the Type of data that can be received. This must match the content-type of the server. The type sent by the server must be the same as the type received by the client. Accept-encoding, accept-language, etc. Content-type: Specifies the Type of the Content to be sent. This Type is usually found in the POST method because the parameters of the POST method are in the request body. Request headers can also add new fields themselves, such as common add token, userId, etc.

  3. A blank line

  4. The request body usually contains data only in the request body of the POST method.

The response

  1. The status line contains the protocol, protocol version, and status code. 1. XXX: data is received. 2. XXX: Data has been received and can be parsed. 3. XXX: redirect. 4. XXX: indicates a client error. 5. XXX: The server is faulty. These are all HTTP status codes, but in fact, the background defines its own error code, and this is not the same as the status code here.

  2. Response header Date: The Date and time when the request was received. Content-type: indicates the data Type of the response. Content-length: indicates the Length of the response data.

  3. A blank line

  4. The response message usually returns HTML web page content or JSON data.

Http Request Process

  1. First, the client sends an HTTP request to connect to the server, and TCP establishes a three-way handshake.
  2. The client starts sending the request data, and the server, such as Java, picks up a thread from the thread pool to receive the HTTP request.
  3. The server takes the request, conducts a resource lookup, and then returns the data to the client. This process is corresponding.
  4. After receiving the data returned by the server, the client saves the data and disconnects the HTTP link four times. At this point, the Java thread processing the request will be put back into the Java thread pool, waiting for the next request.

socket

A socket is a socket that encapsulates TCP/IP. It provides a full-duplex channel for both data transmission. It’s the same as HTTP, where you have three handshakes and four waves to break the link before you transfer the data. The difference with HTTP is that a socket is a long link. HTTP is a short connection. Although the socket is a long link, it may be disconnected due to network reasons. Therefore, a heartbeat packet is usually provided to send a specified heartbeat to the server at regular intervals. If no heartbeat is received at a certain time, it means that the connection has been disconnected.

HTTP long connections are different from sockets

  1. Http1.1 Long connections are made so that the TCP channel can be reused by multiple request servers, thus saving the time of destroying THE TCP and creating the TCP channel again.
  2. The purpose of the socket is to form a channel between the client and the server so that both can communicate with each other.
  3. Http1.1 long connections speed up requests to some extent, but are different from sockets. It was designed differently.