005-TCP Transmission Control Protocol

I. Overview

  Transmission Control Protocol (English: Transmission Control Protocol, abbreviated as TCP) is a connection-oriented, reliable, byte stream-based transport layer communication protocol, defined by RFC 793 of the IETF. In the simplified OSI model of computer network, it completes the functions specified by the fourth layer transport layer, and the User Datagram Protocol (UDP) is another important transport protocol within the same layer.

  In the Internet protocol suite, the TCP layer is an intermediate layer above the IP layer and below the application layer. Reliable, pipe-like connections are often required between application layers of different hosts, but the IP layer does not provide such a streaming mechanism, but provides unreliable packet exchange.

  The application layer sends a data stream represented by 8-bit bytes for Internet transmission to the TCP layer, and then TCP partitions the data stream into segments of appropriate length (usually controlled by the data link layer of the network to which the computer is connected). maximum transmission unit (MTU) limit). Then TCP passes the result packet to the IP layer, which transmits the packet through the network to the TCP layer of the receiving entity. In order to ensure that no packet loss occurs, TCP gives each packet a sequence number, and the sequence number also ensures the orderly reception of packets transmitted to the receiving entity. The receiving entity then sends back a corresponding acknowledgment (ACK) for the successfully received packet; if the sender entity does not receive an acknowledgment within a reasonable round-trip delay (RTT), then the corresponding packet is assumed to have been Losses will be retransmitted. TCP uses a checksum function to check data for errors; checksums are calculated both when sending and receiving.

  Data is called Stream at the TCP layer, and data packets are called Segments. For comparison, data is called Datagram at the IP layer, and data packets are called Fragments. Packets in UDP are called Messages.

  Data frame (Frame): It is an information unit whose starting point and destination point are both the data link layer.
  Packet: It is also a unit of information whose origin and destination are the network layer.
  Datagram: Usually refers to a unit of information at the network layer whose origin and destination both use connectionless network services.
  Segment (Segment): Usually refers to the information unit whose origin and destination are both transport layer.

  The PDU of the data link layer is called Frame (frame);
  the PDU of the network layer is called Packet (data packet);
  TCP is called Segment (data segment);

2. TCP as a whole, three handshakes, four waves

2.1, tcp/ip overall overview

  

  It can be seen from the above figure

  

2.2, TCP packet structure

offset Rank 0–3 4–7 8–15 16–31
0 source connection port destination port
32 Serial number [seq]
64 Confirmation number [ack]
96 header length Reserve marker window size
128 checksum emergency pointer
160 option field
160/192+  
data

 2.3. Detailed overview

Source port (16 bits long) - Identifies the send port

Destination Port (16 bits long) - Identifies the receiving port

sequence number (seq, 32 bits long)

  If a synchronization flag (SYN) is included, this is the original sequence number; the sequence number of the first data bit is incremented by one.
  If there is no synchronization flag (SYN), this is the sequence code for the first data bit.

acknowledgment number (ack, 32 bits long) - The starting sequence number of the data expected to be received. That is, the byte length of the received data is incremented by 1.

Header Length (4 bits long) - The offset value of the start address of the data segment calculated in units of 4 bytes.

Reserved - must be set to 0

marker

  URG—1 means high-priority packets, and the urgent pointer field is valid.
  ACK—1 means the confirmation number field is valid
  PSH—1 means it is data with the PUSH flag, indicating that the receiver should hand over this segment to the application layer as soon as possible without waiting for the buffer to fill up.
  RST - A value of 1 indicates a serious error. It may be necessary to reproduce the creation of the TCP connection. It can also be used to reject illegal segments and reject connection requests.
  SYN - 1 indicates that this is a connection request or a connection acceptance request, which is used to create a connection and synchronize the sequence number.
  FIN - 1 indicates that the sender has no data to transmit, and requests to release the connection.

Window (WIN, 16 bits long)—Indicates the number of bytes that the receiver of this message can receive from the confirmation number, that is, the size of the receiving window. for flow control.

Checksum (Checksum, 16 bits long) - Calculated in 16-bit words for the entire TCP segment, including the TCP header and TCP data. This is a mandatory field.

Urgent Pointer (16 bits long) - The sequence number of the last byte of urgent data in this segment.

Options Field - Up to 40 bytes. At the beginning of each option is a 1-byte kind field that describes the type of option.

  0: End of options table (1 byte)
  1: No operation (1 byte) for word boundary alignment between option fields.
  2: The maximum segment size (4 bytes, Maximum Segment Size, MSS) usually indicates this option in the data packet where the connection is created and the SYN flag is set, indicating the maximum length of the segment that the local end can receive. Usually, the MSS is set to (MTU-40) bytes, and the length of the IP datagram carrying the TCP segment will not exceed the MTU, thereby avoiding IP fragmentation on the machine. Can only appear in synchronization segments, otherwise it will be ignored.
  3: Window expansion factor (4 bytes, wscale), value 0-14. The number of bits used to shift the value of the TCP window to the left, multiplying the window value. Can only appear in synchronization segments, otherwise it will be ignored. This is because the length of the current TCP receive data buffer (receive window) is usually greater than 65535 bytes.
  4: sackOK - The sender supports and agrees to use the SACK option.
  5: Option for SACK to actually work.
  8: Timestamp (10 bytes, TCP Timestamps Option, TSopt) Timestamp
  of the sender (Timestamp Value field, TSval, 4 bytes)
  Timestamp Echo Reply field (Timestamp Echo Reply field, TSecr, 4 bytes)

Padding: This is to make the entire header length an integer multiple of 4 bytes

other options:

  Window expansion: occupies 3 bytes, one of which represents the shift value S. The new window value is equal to the number of window bits in the TCP header increased to (16 + S), which is equivalent to shifting the window value to the left by S bits After getting the actual window size

  Timestamp: Occupies 10 bytes, of which the most important field is the timestamp value field (4 bytes) and the timestamp echo reply field (4 bytes)

  Select confirmation: The receiver has received two 2 bytes that are not continuous with the previous byte stream. If the sequence numbers of these bytes are all within the receiving window, then the receiver will accept the data first, but the information must be accurate Tell the sender to stop sending the received data again

2.4, three stages

2.4.1, link establishment [three-way handshake]

  diagram

    

  Process:    

    1"cs: Client A's TCP sends a connection request segment to server B, the synchronization bit in its header is SYN = 1, and the sequence number seq = x is selected, indicating the sequence number of the first data byte when transmitting data is x

      The client enters the SYN_SEND state and waits for the confirmation from the server;

    2"sc: After the TCP of server B receives the connection request segment, if it agrees, it will send back confirmation (server B should make SYN = 1 in the confirmation segment, make ACK = 1, and its confirmation number ack = x﹢1, the sequence number you choose seq = y)

      Server enters SYN_RECV state;

    3" cs: After client A receives this segment, it gives confirmation to server B, its ACK = 1, and the confirmation number ack = y﹢1 (client A's TCP notifies the upper-layer application process that the connection has been established and the service After receiving the confirmation from client host A, the TCP of end B also informs its upper application process that the TCP connection has been established)

      Both the client and the server enter the ESTABLISHED state

     At this point, the three-way handshake is completed, and you can enter the next stage

 2.4.2, data transmission

 2.4.3. Connection release [Four times wave]

  Diagram:

    

  step:

    tcp waves four times, and since the TCP connection is full duplex, each direction must be closed individually.

    Since TCP connections are full-duplex, each direction must be closed individually. The principle is that when a party completes its data transmission task, it can send a FIN to terminate the connection in this direction. Receiving a FIN only means that there is no data flow in this direction, a TCP connection can still send data after receiving a FIN. The side that shuts down first will perform an active shutdown, while the other side performs a passive shutdown.    

    1" Client A sends a FIN to close the data transfer from client A to server B (segment 4) (A releases the connection FIN = 1 in the header of the segment, its serial number seq = u, and waits for B's confirmation )

      Host A enters the FIN_WAIT_1 state; this means that host A has no data to send to host B;

    2"B sends an acknowledgment, the acknowledgment number ack = u+1, and the segment's own sequence number seq = v (the TCP server process notifies the high-level application process. The connection from A to B in this direction is released, and the TCP connection is in a half-closed state .B if sending data, A still has to receive)

      Host A enters the FIN_WAIT_2 state; Host B tells Host A that I have no more data to send, and the connection can be closed;

    3" Host B sends a FIN segment to host A, requesting to close the connection, and host B enters the CLOSE_WAIT state; if B has no data to send to A, its application process notifies TCP to release the connection

    4"A must send an acknowledgment after receiving the connection release segment. In the acknowledgment segment, ACK = 1, the confirmation number ack=w﹢1, and its own sequence number seq = u + 1

   TCP uses four waved hands to close the connection .

  This is because when the SOCKET in the LISTEN state of the server receives the connection establishment request of the SYN message, it can send ACK and SYN (ACK acts as a response, and SYN acts as a synchronization) in one packet to send. But when closing the connection, when receiving the other party's FIN message notification, it just means that the other party has no data to send to you; but not all your data is sent to the other party, so you may not necessarily close SOCKET immediately, That is, you may also need to send some data to the other party, and then send a FIN message to the other party to indicate that you agree that the connection can now be closed, so the ACK message and the FIN message here are sent separately in most cases.

More extensions: https://www.cnblogs.com/yueminghai/p/6646043.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324789527&siteId=291194637