Data Communication Computer Network 简明教程
Transmission Control Protocol
传输控制协议 (TCP) 是互联网协议套件中最重要的协议之一。它是通信网络(如互联网)中用于数据传输的最广泛使用的协议。
The transmission Control Protocol (TCP) is one of the most important protocols of Internet Protocols suite. It is most widely used protocol for data transmission in communication network such as internet.
Features
-
TCP is reliable protocol. That is, the receiver always sends either positive or negative acknowledgement about the data packet to the sender, so that the sender always has bright clue about whether the data packet is reached the destination or it needs to resend it.
-
TCP ensures that the data reaches intended destination in the same order it was sent.
-
TCP is connection oriented. TCP requires that connection between two remote points be established before sending actual data.
-
TCP provides error-checking and recovery mechanism.
-
TCP provides end-to-end communication.
-
TCP provides flow control and quality of service.
-
TCP operates in Client/Server point-to-point mode.
-
TCP provides full duplex server, i.e. it can perform roles of both receiver and sender.
Header
TCP 报头的长度最少为 20 字节,最长为 60 字节。
The length of TCP header is minimum 20 bytes long and maximum 60 bytes.
-
Source Port (16-bits) - It identifies source port of the application process on the sending device.
-
Destination Port (16-bits) - It identifies destination port of the application process on the receiving device.
-
Sequence Number (32-bits) - Sequence number of data bytes of a segment in a session.
-
Acknowledgement Number (32-bits) - When ACK flag is set, this number contains the next sequence number of the data byte expected and works as acknowledgement of the previous data received.
-
Data Offset (4-bits) - This field implies both, the size of TCP header (32-bit words) and the offset of data in current packet in the whole TCP segment.
-
Reserved (3-bits) - Reserved for future use and all are set zero by default.
-
Flags (1-bit each)
NS - 随机数和位由显式拥塞通知信令进程使用。 CWR - 当主机收到带有 ECE 比特位的报文时,它会设置拥塞窗口减小,以确认已收到 ECE。 ECE - 它具有两个含义:如果 SYN 比特位清除为 0,那么 ECE 表示 IP 数据包设置了其 CE(拥塞体验)比特位。如果 SYN 比特位设置为 1,则 ECE 表示该设备具有 ECT 功能。 URG - 它表明紧急指针字段具有重要数据,应予以处理。 ACK - 它表明确认字段具有重要意义。如果 ACK 被清除为 0,它表示数据包不包含任何确认。 PSH - 设置时,它是向接收站发出请求,要求将其(只要一到达)推送(push)到接收应用程序,而不要对其进行缓冲。 RST - 重置标志具有以下特点:它用于拒绝传入连接。它用于拒绝报文分段。它用于重启连接。 SYN - 此标志用于在主机之间建立连接。 FIN - 此标志用于释放连接,此后不再交换数据。由于具有 SYN 和 FIN 标志的数据包具有序号,因此将按正确顺序对其进行处理。
NS - Nonce Sum bit is used by Explicit Congestion Notification signaling process. CWR - When a host receives packet with ECE bit set, it sets Congestion Windows Reduced to acknowledge that ECE received. ECE -It has two meanings: If SYN bit is clear to 0, then ECE means that the IP packet has its CE (congestion experience) bit set. If SYN bit is set to 1, ECE means that the device is ECT capable. URG - It indicates that Urgent Pointer field has significant data and should be processed. ACK - It indicates that Acknowledgement field has significance. If ACK is cleared to 0, it indicates that packet does not contain any acknowledgement. PSH - When set, it is a request to the receiving station to PUSH data (as soon as it comes) to the receiving application without buffering it. RST - Reset flag has the following features: It is used to refuse an incoming connection. It is used to reject a segment. It is used to restart a connection. SYN - This flag is used to set up a connection between hosts. FIN - This flag is used to release a connection and no more data is exchanged thereafter. Because packets with SYN and FIN flags have sequence numbers, they are processed in correct order.
-
Windows Size - This field is used for flow control between two stations and indicates the amount of buffer (in bytes) the receiver has allocated for a segment, i.e. how much data is the receiver expecting.
-
Checksum - This field contains the checksum of Header, Data and Pseudo Headers.
-
Urgent Pointer - It points to the urgent data byte if URG flag is set to 1.
-
Options - It facilitates additional options which are not covered by the regular header. Option field is always described in 32-bit words. If this field contains data less than 32-bit, padding is used to cover the remaining bits to reach 32-bit boundary.
Addressing
两个远程主机之间的 TCP 通信是通过端口号(TSAP)完成的。端口号的范围是 0 到 65535,分为:
TCP communication between two remote hosts is done by means of port numbers (TSAPs). Ports numbers can range from 0 – 65535 which are divided as:
-
System Ports (0 – 1023)
-
User Ports ( 1024 – 49151)
-
Private/Dynamic Ports (49152 – 65535)
Connection Management
TCP 通信以服务器/客户端模式工作。客户端发起连接,服务器要么接受要么拒绝连接。使用三向握手来进行连接管理。
TCP communication works in Server/Client model. The client initiates the connection and the server either accepts or rejects it. Three-way handshaking is used for connection management.
Establishment
客户端发起连接并发送带有序号的报文分段。服务器用自己的序号对其进行确认,并对客户端的报文分段进行 ACK,其值比客户端的序号大 1。客户端在收到其报文分段的 ACK 后,对服务器的响应发送确认。
Client initiates the connection and sends the segment with a Sequence number. Server acknowledges it back with its own Sequence number and ACK of client’s segment which is one more than client’s Sequence number. Client after receiving ACK of its segment sends an acknowledgement of Server’s response.
Bandwidth Management
TCP 使用窗口大小的概念来满足带宽管理的需求。窗口大小告知远程端的发送方,此端接收方可以接收的数据字节段数。TCP 使用窗口大小 1 通过慢启动阶段,并在每次成功通信后以指数方式增加窗口大小。
TCP uses the concept of window size to accommodate the need of Bandwidth management. Window size tells the sender at the remote end, the number of data byte segments the receiver at this end can receive. TCP uses slow start phase by using window size 1 and increases the window size exponentially after each successful communication.
例如,客户端使用窗口大小 2,并发送 2 个字节的数据。当收到此段的确认时,窗口大小会加倍为 4,而下一次发送的段长度将为 4 个数据字节。当收到 4 字节数据段的确认后,客户端将窗口大小设置为 8,依此类推。
For example, the client uses windows size 2 and sends 2 bytes of data. When the acknowledgement of this segment received the windows size is doubled to 4 and next sent the segment sent will be 4 data bytes long. When the acknowledgement of 4-byte data segment is received, the client sets windows size to 8 and so on.
如果错失确认(即数据在传输网络中丢失或收到 NACK),则窗口大小将减半,并重新启动慢启动阶段。
If an acknowledgement is missed, i.e. data lost in transit network or it received NACK, then the window size is reduced to half and slow start phase starts again.
Error Control &and Flow Control
TCP 使用端口号来了解它需要将数据段移交到哪个应用程序进程。除此之外,它还使用序列号来与远程主机同步。所有数据段都带有序列号进行发送和接收。发送方通过获得 ACK 时,了解接收方收到的上一个数据段是什么。接收方通过查阅最近收到的数据包的序列号而了解发送方发送的上一个数据段。
TCP uses port numbers to know what application process it needs to handover the data segment. Along with that, it uses sequence numbers to synchronize itself with the remote host. All data segments are sent and received with sequence numbers. The Sender knows which last data segment was received by the Receiver when it gets ACK. The Receiver knows about the last segment sent by the Sender by referring to the sequence number of recently received packet.
如果最近收到的数据段的序列号与接收方预期的序列号不匹配,则该段将被丢弃,并发送回 NACK。如果两个段带有相同的序列号到达,则将比较 TCP 时间戳值以做出决策。
If the sequence number of a segment recently received does not match with the sequence number the receiver was expecting, then it is discarded and NACK is sent back. If two segments arrive with the same sequence number, the TCP timestamp value is compared to make a decision.
Multiplexing
将两个或多个数据流合并到一个会话中的技术称为多路复用。当 TCP 客户端与服务器初始化连接时,它始终会指代一个明确的端口号,该端口号表示该应用程序进程。客户端本身使用从专用端口号池中随机生成的端口号。
The technique to combine two or more data streams in one session is called Multiplexing. When a TCP client initializes a connection with Server, it always refers to a well-defined port number which indicates the application process. The client itself uses a randomly generated port number from private port number pools.
通过使用 TCP 多路复用,一个客户端可以在单个会话中与多个不同的应用程序进程通信。例如,客户端请求一个网页,该网页包含不同类型的数据(HTTP、SMTP、FTP 等),TCP 会话超时被增加,会话将被保持开放更长时间,从而可以避免三路握手开销。
Using TCP Multiplexing, a client can communicate with a number of different application process in a single session. For example, a client requests a web page which in turn contains different types of data (HTTP, SMTP, FTP etc.) the TCP session timeout is increased and the session is kept open for longer time so that the three-way handshake overhead can be avoided.
这使用户系统能够通过单个虚拟连接接收多个连接。如果超时时间太长,这些虚拟连接并不适用于服务器。
This enables the client system to receive multiple connection over single virtual connection. These virtual connections are not good for Servers if the timeout is too long.
Congestion Control
当系统中输入大量的数据而系统无法处理时,就会发生拥塞。TCP 通过窗口机制来控制拥塞。TCP 设置窗口大小,告诉另一端发送多少数据段。TCP 可使用三种算法来控制拥塞:
When large amount of data is fed to system which is not capable of handling it, congestion occurs. TCP controls congestion by means of Window mechanism. TCP sets a window size telling the other end how much data segment to send. TCP may use three algorithms for congestion control:
-
Additive increase, Multiplicative Decrease
-
Slow Start
-
Timeout React
Timer Management
TCP 使用不同类型的计时器来控制和管理各种任务:
TCP uses different types of timer to control and management various tasks:
Keep-alive timer:
-
This timer is used to check the integrity and validity of a connection.
-
When keep-alive time expires, the host sends a probe to check if the connection still exists.
Retransmission timer:
-
This timer maintains stateful session of data sent.
-
If the acknowledgement of sent data does not receive within the Retransmission time, the data segment is sent again.
Persist timer:
-
TCP session can be paused by either host by sending Window Size 0.
-
To resume the session a host needs to send Window Size with some larger value.
-
If this segment never reaches the other end, both ends may wait for each other for infinite time.
-
When the Persist timer expires, the host re-sends its window size to let the other end know.
-
Persist Timer helps avoid deadlocks in communication.
Timed-Wait:
-
After releasing a connection, either of the hosts waits for a Timed-Wait time to terminate the connection completely.
-
This is in order to make sure that the other end has received the acknowledgement of its connection termination request.
-
Timed-out can be a maximum of 240 seconds (4 minutes).
Crash Recovery
TCP 是一个非常可靠的协议。它为每个段中发送的字节提供序列号。它提供反馈机制,即当主机收到一个数据包时,它有义务对具有下一期望序列号(如果不是最后一个段)的数据包发送 ACK。
TCP is very reliable protocol. It provides sequence number to each of byte sent in segment. It provides the feedback mechanism i.e. when a host receives a packet, it is bound to ACK that packet having the next sequence number expected (if it is not the last segment).
当一个 TCP 服务器在通信途中崩溃并重新开始其进程时,它会向其所有主机发送 TPDU 广播。然后,主机可以发送从未确认且带有的最后一个数据段继续接收。
When a TCP Server crashes mid-way communication and re-starts its process it sends TPDU broadcast to all its hosts. The hosts can then send the last data segment which was never unacknowledged and carry onwards.