Understand protocols and TCP/IP network protocols

Protocol, short for network protocol, is the rules that both sides of a communication computer must follow. At every layer of the Internet, there are many protocols defined.

A network model

  • Seven-layer network model

Application layer: Provides services to the application and specifies the details related to communication within the application. Protocols include file transfer, email, and remote login (virtual terminal). Presentation layer: mainly responsible for data format conversion. To convert information processed by an application into a format suitable for network transmission, or to convert data from the next layer into a format that the upper layer can process. Session layer: Communication management, responsible for establishing and disconnecting communication connections (logical paths through which data flows), as well as data transmission related management, such as data segmentation. Transport layer: plays the role of reliable transport. It is processed only on the communication nodes, not on the router. Network layer: transfers data to destination address. Address addressing and routing. Data link layer: responsible for the communication transmission between nodes interconnected on the physical layer. Physical layer: responsible for 0, 1 bit flow (0, 1 sequence) and the high and low voltage, light flashing between the exchange.Copy the code
  • TCP/IP architecture

From the client to the server, the HTTP request goes through a series of processes. In a nutshell:

From sending HTTP requests at the application layer, to establishing TCP/IP connections through three-way handshake at the transport layer, to IP addressing at the network layer, to encapsulation into frames at the data link layer, and finally to the physical layer using physical media transmission.

1. Application layer (DNS, HTTP) The DNS resolves to AN IP address and sends an HTTP request 2. Transport layer (TCP, UDP) Establishing a TCP connection (three-way handshake) 3. Network layer (IP,ARP) IP address 4. 5. Physical layer (using physical media to transmit bit stream) Physical transmission (then transmission through twisted pair, electromagnetic wave and other media)Copy the code

Above,TCP/IPArchitecture, the diagram contains protocols for each layer.TCPTreat the connection as the most basic object, each oneTCPA connection has two endpoints. This breakpoint, called a socket, is defined as a port number concatenated toIPThe address constitutes a socket, for example, ifIPAddress is192.3.4.16, and the port number is80, then the resulting socket is192.3.4.16:80.

Network communicationThe exchange of packets is a process on one host exchanging data with a process on another host. For example, computer A sends A packet to computer B. The latter receives it and replies with A packet, thus realizing the communication between the two computers. The structure of the packet is basically as follows:Understand HTTP transaction processing:

Three-way handshake

TCP packet header (Brief introduction of related packets)

Confirm ACK. The confirmation number field is valid only when ACK=1. According to TCP, ACK must be set to 1 for all packets transmitted after the connection is established. Syncing SYN, used to synchronize serial numbers when a connection is established. If SYN=1,ACK= 0, it indicates a connection request packet. If SYN=1,ACK=1 is accepted, the response packet should be SYN=1,ACK=1. Terminates the FIN to release the connection. If the FIN value is 1, the sender of the packet has finished sending data and wants to release the data.Copy the code

TCP establishment (three-way handshake)

In the beginning both the client and the server are inCLOSEDState. The client actively opens the connection, while the server passively opens the connection.

  • The TCP server process creates the transport control block firstTCB, ready to accept a connection request from the client process, at which point the server entersLISTEN (LISTEN)State.
  • TCP client processes also create transport control blocks firstTCB, and sends a connection request message to the server. At this time, the synchronization bit of China is in the header of the messageSYN=1, and select an initial serial numberseq=x; At this point, the TCP client process entersSyn-sent (Synced SENT)State.According to TCP, the SYN segment (SYN=1) cannot carry data, but must consume a sequence number.
  • After receiving the request packet, the TCP server sends an acknowledgement packet if it agrees to the connection. The value in the confirmation packet should beACK = 1, SYN = 1, the confirmation number isack=x+1, also initialize a serial number for yourselfseq=yAt this point, the TCP server process entersSyn-rcvd (Synchronously received)State.This message also does not carry data, but again consumes a serial number.
  • The TCP client process also sends an acknowledgement to the server after receiving the acknowledgement. Acknowledgement of messageACK = 1, ACK = y + 1, his serial numberseq=x+1, the TCP connection is established and the client entersESTABLISHED (the connection has been ESTABLISHED)State.According to TCP, AN ACK packet segment can carry data, but does not consume serial numbers if it does not.
  • When the server receives the client’s acknowledgement, it also entersESTABLISHEDState, after which the two parties can start communicating.

The following GIF shows the process of three handshakesBegan to draw, draw it a few times and remember it.

  • Three handshakes dialogue simulation:
    • Client: Are you the XXX server?
    • Server: I am the XXX server, are you the client?
    • C: Yes, I am the client
    • After the connection is established, formal data transfer can proceed.

Why does the TCP client send an acknowledgement at the end?

The main purpose is to prevent invalid connection request packets from suddenly being sent to the server, resulting in errors. If two handshakes are used to establish a connection, suppose that the client sends a connection request and does not lose it, but stays in the network node for a long time. Because the TCP client does not receive the acknowledgement packet, it thinks that the server has not received it, and sends the packet to the server again. Since the client and the server after two handshake complete connection, data transmission, and then close the connection, stranded that a request to connect, network flow to the server, the message was supposed to be failure, however, two handshake mechanism will let the client and the server to connect again, this will lead to unnecessary mistakes and waste of resources. If the three-way handshake is used, even if the invalid packet is sent, the server receives the invalid packet and replies with an acknowledgement message, but the client does not send an acknowledgement again. Since the server does not receive an acknowledgement, it knows that the client has not requested a connection.

Four times to wave

TCP release (four waves)

  • The client process sends a connection release packet and stops sending data. Release the data header,FIN=1And its serial number isseq=uAt this point, the client entersFin-wait-1 (terminate WAIT 1)State.According to TCP, FIN packets consume a sequence number even if they do not carry data.
  • The server receives a connection release packet and sends an acknowledgement packet.ACK = 1, ACK = u + 1And bring your own serial numberseq=v, the server entersClose-wait (CLOSE WAIT)State.TCPThe server notifies the higher-level application process, and the client is released to the server, which is in a half-closed state,That is, the client has no data to send, but if the server sends data, the client still needs to receive it. It’s going to last for a while, which is the wholeCLOSE-WAITDuration of the state.
  • When the client receives an acknowledgement request from the server, the client entersFin-wait-2 (terminate WAIT 2)Waiting for the server to send a connection release packet (Before that, you need to receive the final data sent by the server).
  • After sending the final data, the server sends a connection-release message to the client,FIN = 1, ack = u + 1Since the server is in the semi-closed state, it is likely that the server sent some more data, assuming that the serial number isseq=wAt this point, the server entersLast-ack (final confirmation)Status waiting for client confirmation.
  • After receiving the connection release packet from the server, the client must send an acknowledgement message.ACK = 1, ACK = w + 1And his serial number isseq=u+1At this point, the client entersTime-waitstateNotice The TCP connection is not released and must be passed2*MSL(Maximum packet segment life). The client enters the CLOSED state only after revoking the corresponding TCB.
  • Once the server receives an acknowledgement from the client, it immediately enters the serverCLOSEDState. Similarly, revoking the TCB terminates the TCP connection. As you can see,The server ends the TCP connection earlier than the client.

The following GIF shows the process of four wavesBegan to draw)

  • Four waves to disconnect
    • Client (active) : I have closed your message sending channel and can only passively receive messages
    • Server (passive) : I received the message that the channel is closed.
    • Server (passive) : I have now also closed the channel for sending messages to you.
    • Client (active party) : Received your message, disconnect. The two sides could no longer communicate.

Why does the client end up waiting for 2MSL?

MSL (Maximum Segment Lifetime) : TCP allows different implementations to set different MSL values.

  • First, ensure that the client sends the final ACK packet to reach the server, because the ACK packet may be lost, standing in the server’s perspective, I have already sent the FIN + ACK message request disconnect, the client also did not give me response, should be I send the request of the disconnect message it did not receive, then the server will send a again, The client receives the retransmitted message within the 2MSL period, responds with a message, and restarts the 2MSL timer.
  • Second, prevent “invalid connection request message segment” as mentioned in “three-way handshake” from appearing in this connection. After the client sends the last acknowledgement message, in this 2MSL time, all the message segments generated during the duration of the connection can be removed from the network. In this way, the new connection does not contain the request packets of the old connection.

Why is establishing a connection a three-way handshake, but closing a connection a four-way wave?

During connection establishment, the server receives a SYN packet in LISTEN state and sends the ACK and SYN packets to the client. And close connection, the server receives the other side of the FIN message, just said to each other can no longer send data but also receives the data, and the oneself also is not necessarily all data are sent to each other, so their can immediately shut down, also can send some data to each other, then send the FIN message now agreed to close the connection to the other side, therefore, The ACK and FIN are usually sent separately, resulting in an extra ACK.

What if the connection has been established, but the client suddenly fails?

TCP also has a keepalive timer, so obviously if the client fails, the server can’t wait forever and waste resources. The server resets this timer every time it receives a request from the client, usually for two hours. If it does not receive any data from the client within two hours, the server sends a probe segment, which is then sent every 75 seconds. If there is no response after 10 probe packets are sent, the server assumes that the client is faulty and closes the connection.

Common status code

  • Meanings of different ranges of states:
1xx -- indicating message, request received, continue processing 2XX -- success, request received, understood, accepted 3XX -- redirection, further action must be taken to complete the request 4XX -- client error, request has syntax error or request cannot be implemented 5XX -- server error, The server failed to fulfill a valid requestCopy the code
  • The most commonly used is the status code. Most of the time, it is judged by the status code. For example, (to list some of the most common ones) :
100 -- to continue. The client should continue with its request. 200 -- indicates that the request was successfully completed and the requested resource is sent back to the client 301 -- permanent redirect, the requested resource has been permanently moved to a new URI 302 -- temporary redirect, resource knowledge has been temporarily moved, The client should continue to use URI 304 -- the requested page has not changed since the last request, Ask client to use local cache 400 -- client request error (for example, it can be blocked by security module) 401 -- unauthorized request 403 -- Access forbidden (for example, it can be forbidden when not logged in) 404 -- Resource not found 500 -- server internal error 503 -- service unavailable...Copy the code
  • 301 Permanent redirect and 302 temporary redirect
    • 301 permanent redirection is the best method for search engines (SEO) after a web page has changed its address, as long as it is not temporarily moved, 301 is recommended for redirection. If we use a 301 jump to an address, the search engine will transfer the original external links under the old address to the new address, while completely abolishing the original old address in the search engine. Will not affect the site’s ranking.

      Suppose you are permanently redirected to b.com when you visit a.com; Then the next time you visit a.com, the browser will go directly to b.com instead of asking for a.comCopy the code
    • 302 temporary redirection, generally for search engines, 302 will not transfer weight, after login to the home page jump operation with 302. This will result in a lower ranking of the site.

      If you visit a.com and are temporarily redirected to b.com; So the next time you ask for a.com the browser will ask for a.com first and then redirect to b.comCopy the code

UDP&TCP

UDP

  • No connection; Unreliable; Message oriented; No congestion control; High transmission efficiency; Support one-to-one, one-to-many, many-to-one, many-to-many interactive communication;
  • Application scenario: High transmission efficiency but low accuracy. For example, video and audio require high real-time performance, broadcast communication, etc.

TCP

  • Connection-oriented; Reliable; Byte oriented stream; Full-duplex communication mode; Slow transmission efficiency;
  • Application scenario: When the network communication quality is required, for example, the entire data must be transmitted to the peer party accurately, the data is used for reliable applications, such as HTTP, HTTPS, and FTP. Mail transfer protocol such as SMTP.
  • How to ensure reliable transmission?
    • TCP achieves reliability through sequence number, checksum, acknowledgement acknowledgement, resend control, connection management, window control, flow control, and congestion control.

TCP reliable transmission

You can read this article: How does TCP guarantee reliable transmission

— — — — — — — —

  • Reference document 1: original article by CSDN blogger “Little Book Go”
  • Reference 2