This paper summarizes the network knowledge.

1. Network basics

1.1 Differences between GET and POST Requests

POST and GET are two HTTP request modes. The differences are as follows:

  • Application scenario: A GET request is an idempotent request. Generally, A GET request does not affect the resource server, whereas a POST request is a non-idempotent request.
  • Cache or not: Browsers typically cache GET requests, but rarely POST requests.
  • The format of the sent packet is GET request, and the entity part is empty. The entity part of a POST request packet sends data to the server.
  • Security: The GET interface request parameters are placed in the URL, which is not secure for POST requests because the accessed records are kept in the history.
  • Request length: The length of get requests sent is affected by browser url restrictions. This is a browser limitation, not an RFC limitation.
  • Request parameters: POST supports more request parameters.

Note: Idempotent request: An HTTP method is idempotent, meaning that the same request executed once has the same effect and the state of the server is the same as if it were executed multiple times in a row. In other words, idempotent methods should not have side effects (except for statistical purposes). Methods like GET, HEAD, PUT, and DELETE are idempotent when implemented correctly, but POST is not.

1.2 Differences between POST and PUT Requests

  • A PUT request sends data to the server to modify the content of the data, but does not increase the type of data. That is, the result is the same no matter how many times the data is PUT. (Update data)
  • A POST request sends data to the server to change the resource and type of data, and it creates a new resource. (New data)

1.3 Common HTTP Request and Response Headers

The HTTP request header has the following:

  • Referer: Making the request page URL
  • Accept-charset: Content that the browser can process
  • Accept-encoding: Character set that can be displayed by the browser
  • Connection: the Connection between the browser and the server
  • Cookie: Cookie set on the current page
  • Host: indicates the domain name of the current page
  • Referer: Url for sending page requests
  • User-agent: indicates the character string of the browser User Agent

HTTP response headers:

  • Date: indicates the time when a user sends a message
  • Server: indicates the Server name
  • Cache-control: controls the Cache
  • Content-type: indicates the returned document Type

1.4 Differences between HTTP and HTTPS

  • HTTPS requires a CA certificate, which costs a lot. The HTTP protocol does not
  • HTTPS is a hypertext transmission protocol. Information is transmitted in plain text. HTTPS is a secure SSL encryption transmission protocol
  • The port number varies with the connection mode. HTTP is 80 and HTTPS is 443
  • HTTP connections are simple and stateless; HTTPS is a network protocol that uses SSL and HTTP to encrypt transmission and authenticate identity, and is more secure than HTTP.

1.5 TCP three-way handshake

TCP three-way handshake:

  • First handshake: The client sends a SYN packet and waits for confirmation from the server.
  • Second handshake: After receiving a SYN packet, the server needs to send an ACK packet to the client. At the same time, the server also sends a SYN packet, namely, a SYN+ACK packet. This SYN packet is different from the first handshake packet.
  • Third handshake: The client receives a SYN+ACK packet and sends an ACK packet to the server. The client enters the ESTABLISHED state. After the server receives the ACK packet, the server also enters the ESTABLISHED state and completes the three-way handshake.

Extension problem:

Is a second handshake ok?

Before establishing TCP, ensure that the client and server can contain and send packets.

  • First handshake: The client sends a network packet and the server receives the packet. In this way, the server obtains the following conclusions: the sending capability of the client and the receiving capability policy of the server.
  • Conclusion: The sending and receiving capabilities of the server are normal, and the receiving and sending capabilities of the client are normal. However, the client does not know whether the server knows that the receiving capability of the client is normal.
  • Third handshake: The client sends the packet and the server receives the packet. In this way, the server can confirm that the receiving capability of the client is normal.

Taobao second interview, the interviewer incredibly TCP three handshake asked so detailed

1.6 TCP wave four times

  • First wave: The client initiates a FIN packet (FIN = 1) and enters the FIN_WAIT_1 state. TCP specifies that a FIN packet consumes an ordinal number even if it does not carry data.
  • Second wave: After receiving the FIN packet, the server sends an ACK (ACK = u + 1) with its serial number seq = V, and the server enters the CLOSE_WAIT_1 state. At this time, the client has no data to send. If the server has data to send, the client still needs to accept. After receiving ACK and SEQ, the client enters the FIN_WAIT_2 state.
  • Third wave: After sending data, the server sends a FIN packet (SEq = W, ACK = U + 1) to the client. In the semi-connected state, the server may also send other data to the client. If the value is seq = W, the server enters the LAST_ACK state.
  • Fourth wave: After receiving the FIN packet, the scale end sends an acknowledgement packet (ACK = 1, seq = u + 1). At this time, the client enters the TIME_WAIT state and the TCP connection is not released. After 2 * MSL, it enters the CLOSED state. After receiving the confirmation packet, the server enters the CLOSED state. It can be seen that the server disconnects faster than the client.

Extension problem:

Why do you need to shake hands three times to establish a connection and four times to close a connection?

In the TCP handshake, the receiving end combines ACK and SYN packets into one packet. Therefore, one packet is sent and one request is reduced.

For the four-wave, TCP is a full-duplex communication. After the active closing party sends the confirmation packet, the receiving party may also need to receive data. Therefore, the communication channel cannot be closed immediately. So four waves are four packets of interaction.

1.7 the TCP and UDP

TCP and UDP are transport layer protocols and belong to the TCP/IP protocol family:

UDP

UDP: User datagram protocol. In networks, it handles packets and is a connectionless protocol. In OSI, it is one layer above the IP protocol. UDP does not provide the ability to group, assemble, or sort data packets. After packets are sent, you cannot know whether the server receives them successfully.

Features:

  • Connectionless: UDP does not require a three-way handshake like TCP and can be sent at will.
  • Unicast, multicast, broadcast features: UDP supports one-to-many, many-to-many, and many-to-one modes.
  • Packet oriented: The UDP packets sent by the sender to the application are forwarded to the IP layer after the header is added. UDP does not merge or split packets from the application layer, but retains the boundaries of the packets. Therefore, the application must select a message of the appropriate size.
  • Unreliability: UDP does not need to be established, and can be sent at will, which is not reliable.
  • The header overhead is small and the transmission is efficient.

TCP

TCP features:

  • Connection-oriented: Three handshakes are required to establish a reliable connection.
  • Unicast transmission only: EACH TCP connection supports point-to-point connection and transmission, but does not support multicast or broadcast.
  • Reliable transmission: due to the establishment of reliable connection, as well as the corresponding judgment of the loss of code, code, and retransmission mechanism.
  • Provides congestion control: Reduces the amount of data injected into the network when the connection is congested
  • Full-duplex communication: TCP allows applications on both sides of the communication to send data at any time because there are caches at both ends of the TCP connection to temporarily store two-way communication data. Of course, TCP can send a segment immediately, or it can cache for some time to send more segments at once (the maximum segment size depends on MSS)

Learn more about TCP and UDP in this article

1.8 Differences between HTTP2.0 and HTTP1.1

  • Binary protocol: HTTP2.0 is a binary protocol. In HTTP1.1, the header is text (ASCII encoded), and the datagram can be either text or binary. In HTTP2.0, the header is completely binary, and the message body and data body are both binary, collectively referred to as’ frames’, which is the basis of HTTP2.0 multiplexing.
  • Multiplexing: HTTP2.0 implements multiplexing, although HTTP2.0 still uses TCP protocol, but a connection, the client and server can simultaneously send, receive multiple requests, to avoid the ‘queue blocking’ problem
  • Data flow: HTTP2.0 implements the concept of data flow, because HTTP2.0 packets are not in order, the same connection inside the packet, may belong to different requests, need to mark the packet, indicating which request, HTTP2.0 will each request or response of the packet, as data flow.
  • Header compression: HTTP2.0 implements header compression
  • Server push: HTTP2.0 allows servers to push unsolicited resources (static resources) to clients. This is called server push.

1.9 OSI seven-layer model

OSI seven layer model role
The application layer Provide network services for applications
The presentation layer Data formatting, encryption, decryption
The session layer Establish, maintain, and manage calls
The transport layer Establish, maintain, and manage end-to-end connections
The network layer IP addressing and routing
Data link layer Controls the network layer and physical layer connections
The physical layer Bitstream transmission