[Computer Network]-TCP-Overview

TCP overview

RFC document address
TCP protocol is a protocol located at the transport layer in the TCP/IP protocol suite.

TCP/IP four-layer architecture

You can see the two main protocols of the transport layer except TCP ( Transmission Control Protocol Transmission\ Control\ ProtocolT r an s mi ss i o n C o n t ro l P ro t oco l   , Transmission Control Protocol) There is also a UDP (U ser Datagram Protocol User Datagram ProtocolU serD a t a g r am P ro t oco l , User Datagram Protocol)

What is the difference between TCP and UDP

  1. connect: TCP provides connection-oriented , reliable data transmission services; while UDP provides connectionless , best-effort data transmission services and does not guarantee the reliability of transmission.
  2. Service objects: TCP is a one-to-one two-point service, that is, a connection has only two endpoints; UDP supports one-to-one, one-to-many, and many-to-many interactive communication
  3. reliability: TCP delivers data reliably, and the data can be error-free, not lost, not repeated, and arrives on demand; UDP delivers data on a best-effort basis and does not guarantee reliable delivery of data.
  4. congestion control: TCP has congestion control and flow control mechanisms to ensure the security and reliability of data transmission; UDP has no relevant mechanism, and its sending rate will not be affected even if the network is very congested.
  5. head overhead: The TCP header is long and requires 20 bytes when the "option" field is not used ; the UDP header is only 8 bytes and is fixed.
  6. transfer method: TCP is byte stream-oriented, streaming transmission, without boundaries, but ensures the order and reliability of data transmission. Its data transmission unit is a message segment; UDP is sent packet by packet, and has boundaries, but Packet loss and disorder may occur, and the transmission unit is user datagram.

Respective application scenarios:

  • TCP: Since TCP is connection-oriented, it can ensure the reliable delivery of data. It is often used for: HTTP and HTTPS for the World Wide Web; FTP for file transfer; SMTP (Simple Mail Transfer Protocol) for email transfer; TELNET (Remote Terminal Protocol) for remote terminal access
  • UDP: UDP does not require a connection and can send data at any time. It has no congestion control and does not guarantee reliability, so it is often used for: communications with a small total number of packets, such as domain name conversion to DNS, network management SNMP (Simple Network Management Protocol); However, due to its strong real-time nature, it is suitable for real-time applications, such as IP telephony, real-time video conferencing and other scenarios. This kind of scenario has great real-time requirements, and the impact of missing a few frames on the overall data is not great.

It can be seen that the measurement point can consider the amount of data transmitted, the size of the real-time requirements, and the size of the data reliability requirements. Some
application scenarios do not mean that only one of the two protocols, such as DNS, will be used. UDP is sufficient, but there may also be situations where you need to access a domain name server that is far away, in which case you may need to use TCP.

TCP's service model

TCP provides a connection-oriented , reliable byte stream service
. It is connection-oriented, that is, two applications using TCP must establish a TCP connection before the two parties can exchange data .

In the byte stream abstraction:

  1. There are no record flags or message boundaries automatically inserted by TCP . When both parties of the connection read and write data, the number of reads or writes and the size of each read or write are selected by the endpoint itself.
  2. TCP does not interpret the contents of the bytes in the byte stream. It does not know whether the data bytes being exchanged are binary data, ASCII characters or something else. Interpretation of the byte stream depends only on the application of the connected endpoint

TCP reliability

TCP achieves reliable transmission through checksums, sequence numbers, confirmation responses, connection management, timeout retransmission , sliding window mechanism, flow control , congestion control and other mechanisms.

  • Because TCP provides a byte stream interface, it must convert a byte stream from a sending application into packets that IP can carry. This is called a packet . These packets containserial number, the sequence number represents the byte offset of the first byte of each packet in the entire data stream , which allows the packet to be of variable size during transmission and allows reassembly of the packet . The data block transmitted from TCP to IP is called a segment .segment)
  • If a carry is invalidChecksumsegment arrives, then TCP will discard it and not send any acknowledgment for the discarded packet.
  • When TCP sends a group of message segments, it will set a retransmission timer , and then wait for the other party to send a confirmation ACK that the message segment has been successfully received . If an ACK is not received in time, the corresponding message segment will beRetransmission. When TCP receives data from the other end of the connection, it sends an ACK. However, TCP uses cumulative acknowledgment , that is, an ACK indicating byte number N, implying that all bytes up to sequence number N (excluding N) have been successfully received , which brings certain problems to ACK loss. Robustness, if an ACK is lost, it is very likely that the subsequent ACK will be enough to confirm the previous message segment, avoiding the impact of the previous ACK loss.
  • TCP provides full-duplex communication . Data can flow in two directions, and the two directions are independent of each other. Segments in each direction send their own ACKs, including window advertisements to achieve the opposite direction.flow control
  • useserial number, a TCP receiver can discard duplicate segments and record segments that arrive in disordered order . Because TCP uses IP to deliver its message segments, IP does not provide the functions of deduplication and correct ordering . TCP is a byte stream protocol and will never send data to the receiving application in a messy order. Therefore, the receiving end will be forced to save the data with large sequence numbers first and not hand it over to the application until the small missing parts in the middle are The message segment with the sequence number is received, fills the gap in the entire byte stream, and then continues to send the byte stream data.

TCP header and encapsulation

The encapsulation of TCP in IP datagram is as follows:
TCP encapsulation in IP datagrams

The TCP header is shown in the figure:
TCP header

  1. Each TCP header contains a source port and a destination port . These two values ​​​​and the source IP address and destination IP address in the IP header . This four-tuple uniquely represents each connection . The combination of an IP address and a port is sometimes called an endpoint .e n d p o in t ) orsocket(socket socketsocket)

  2. The sequence number is a 32-bit unsigned number. Each message segment corresponds to a sequence number. It represents the sequence number of the first byte in the data of the message segment, because TCP will give the sequence number between the two applications. Each byte in the communication data stream is assigned a sequence number (the data stream considered here only considers one direction, and the two directions of a connection are independent of each other). The sequence number reaches 2 32 - 1 and then loops back to 0

  3. The value contained in the confirmation number ACK isThe next sequence number that the sender of this confirmation number expects to receive,AlsoThe sequence number of the last successfully received data byte is incremented by 1, it can also be understood that for the communicating party that sends this ACK, all sequence numbers before this sequence number (excluding this sequence number) have been received.

    This field is only valid when the ACK bit field is enabled, and this ACK bit field is usually in a connection, except for the initial message segment and the last message segment. Will be enabled in other segments

    Since ACK is part of the segment header, sending an ACK costs the same as sending any message.

  4. The header length field gives the length of the header. Because the length of the selection field in the header is variable except for the basic part, which is fixed at 20 bytes, this header length field is necessary in order to know the length of the entire header.

    The header length field occupies a total of 4 bits, and its value is in 32-bit units . That is to say, 0000 indicates that the header length is 0 bits, 0001 indicates that the header length is 32 bits, 0010 indicates that the header length is 64 bits...so The maximum value of 1111 indicates a length of 60 bytes, that is, the maximum length of the TCP header can only be 60 bytes. The basic TCP header is 20 bytes long, which means that the option field can be up to 40 bytes long. It is also because the header length field is in units of 32 bits, so the length of the TCP header should be a multiple of 32 bits.

  5. Reserved The field is reserved for future use and is currently set to all 0s.

  6. 8 control bits are defined in the header :
    – CWR: Congestion Window Decrease (indicates that the sender has reduced its sending rate, see TCP Congestion Control section)
    – ECE: ECN Echo (indicates that the sender has received an earlier congestion notification , see the TCP Congestion Control section)
    – URG: Urgent (the urgent pointer field is valid, rarely used)
    – ACK: Acknowledgment (the confirmation number field is valid, and is generally enabled after the connection is established)
    – PSH: Push (the receiver should as soon as possible Send this data to the application, but this function is not reliably implemented or used)
    – RST: reset the connection (the connection is canceled, often due to an error)
    – SYN: the synchronization sequence number used to initialize a connection
    – FIN: the The sender of the message segment has finished sending data to the other party.

  7. TCP flow control is accomplished by each endpoint advertising a window size using the window size field. The window size is the number of bytes starting from the byte the receiver wants to receive. Since the length of the field is only 16 bits, the maximum window size can only be 65535 bytes , thus limiting the throughput performance of TCP

    but there is onewindow zoomOptions allow scaling of this value, providing a larger window and improved performance for high speed and large latency networks

  8. The checksum field is calculated and saved by the sender and then verified by the receiver

  9. The Urgent Pointer field is only valid when the URG bit is enabled. Its value is a positive offset, and the value obtained after adding it to the sequence number field of the message segment is the sequence number of the last byte of emergency data.

  10. Options fields:
    >>> The most common option is maximum segment sizeMSS ( M a x i m u m   S e g m e n t   S i z e Maximum\ Segment\ Size M a x im u m S e g m e n t S i ze   ). Each endpoint of the connection generally sends the first segment it sends, that is,SYN segmentSpecify this option, MSS specifies the maximum value of the message segment that the sender of this option hopes to receive in the opposite direction
    >>> In addition, there are options such as SACK, timestamp and window scaling.

byte order


The issue discussed in the reference article byte order is, when storing data consisting of more than one byte, should the high-order byte be placed at a low address, the low-order byte be placed at a high address, or should the high-order byte be placed at a high address ? The bytes are placed at the high address, and the low bytes are placed at the low address.

Specifically, it can be divided into Big Endian byte order and Little Endian byte order. Big Endian means that the high-order bytes are placed at low addresses and the low-order bytes are placed at high addresses; Little Endian is the same as on the contrary

A single computer can freely choose whether it wants to choose big endian or little endian. This is called host byte order.

The emergence of the network requires communication between multiple computers. At this time, it is necessary to specify a byte order used for communication to ensure that the communication content is parsed correctly. In TCP, RFC 1700 specifies the use of " big endian " byte order as the network byte order . Other computers that do not use big endian byte order must convert their own host byte order to network byte order when sending data ( That is, "big endian" byte order); the received data is then converted to its own host byte order. In this way, the byte order has nothing to do with the CPU and operating system, realizing the standardization of network communication.

Guess you like

Origin blog.csdn.net/Pacifica_/article/details/123929280