Transmission Control Protocol - Wikipedia
TCP是在不可靠的IP层之上实现的可靠的数据传输协议,它主要解决传输的可靠、有序、无丢失和不重复问题。
TCP是TCP/IP体系中非常复杂的一个协议,主要特点如下:
1)TCP是面向连接的传输层协议,TCP连接是一条逻辑连接。
2)每条TCP连接只能有两个端点,每条TCP连接只能是端到端的(进程对进程)。
3)TCP提供可靠交付的服务,保证传送的数据
4)TCP提供全双工通信,允许通信双方的应用进程在任何时候都能发送数据,为此TCP连接的两端都设有发送缓存和接收缓存,用来临时存放双向通信的数据。
5)TCP是面向字节流的
TCP和UDP在发送报文时所采用的方式完全不同。
UDP报文的长度由发送应用进程决定
TCP报文的长度则根据接收方给出的窗口值和当前网络拥塞程度来决定。
如果应用进程传送到TCP缓存的数据块太长,TCP就把它划分得短一些再传送:
如果太短,TCP也可以等到积累足够多的字节后再构成报文段发送出去。
TCP传送的数据单元(TCP@PDU)称为报文段(segment)。
TCP报文段既可以用来运载数据,又可以用来建立连接、释放连接和应答
一个TCP报文段分为首部和数据两部分
IP数据报
IP首部
数据部分(TCP报文段TCP segment)
TCP首部
20(5*4B)字节固定部分
第1字(1~4B) | 第2字(5~8B) | 第3字(9~12) | 第4字(13~16) | 第5字(17~20) | |
---|---|---|---|---|---|
前2B | 源端口 | 序号 | 确认号 | 数据偏移 @保留 @标志 | 校验和 |
后2B | 目的端口 | 序号 | 确认号 | 窗口 | 紧急指针 |
第4字的前两个字节
40字节可变部分
数据部分
数据偏移
data offset(即首部长度)。 6)紧急位URG
。
紧急指针字段(2Bytes:16bit)
配合使用 7)确认位ACK。
8)推送位PSH(Push)。
9)复位位RST (Reset)。
10)同步位SYN
。
同步SYN=1
表示这是一个连接请求或连接接受报文。
当SYN=1,ACK=0
时,表明这是一个连接请求报文,
SYN= 1,ACK=1
。11)终止位FIN
(Finish)。用来释放一个连接。
发送方的数据
已发送完毕,并要求释放
传输连接。12)窗口window size
13)校验和 checksum
占2B。校验和字段检验的范围包括首部和数据两部分。
在计算校验和时,和UDP一样,要在TCP报文段的前面加上12B的伪首部
(只需将UDP伪首部的第4个字段,即协议字段的17改成6,其他的和UDP一样)。
14)紧急指针
(urgent pointer)
15)选项 options
16)填充(pad)
ack
(4Byte:32bit)(小写的ack)ACK
(1位:1bit)(大写的ACK) MSS
)。[2017统考真题]
若甲向乙发起一个TCP连接,最大段长MSS=1KB,RTT=5ms,
乙开辟的接收缓存rwnd=64KB,则甲从连接建立成功至发送窗口达到twnd=32KB,需经过的时间至少是(A)。
按照慢开始算法,发送窗口=min(拥塞窗口,接收窗口},
初始的拥塞窗口为最大报文段长度1KB。
i | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
rwnd(MSS=1KB) | 64 | 63 | 61 | 57 | 49 | 33 |
cwnd(MSS) | 1 | 2 | 4 | 8 | 16 | 32 |
twnd(MSS) | 1 | 2 | 4 | 8 | 16 | 32 |
rwnd_next | 63 | 61 | 57 | 49 | 33 | 1 |
cwnd_next(MSS) | 2 | 4 | 8 | 16 | 32 | 1 |
每经过一个RTT,拥塞窗口翻倍,因此需至少经过5个RTT,发送窗口才能达到32KB,
这里假定乙能及时处理接收到的数据,空闲的接收缓存≥32KB。
Source port (16 bits)
Identifies the sending port.
Destination port (16 bits)
Identifies the receiving port.
Sequence number (32 bits)
Has a dual role:If the SYN flag is set (1), then this is the initial sequence number. The sequence number of the actual first data byte and the acknowledged number in the corresponding ACK are then this sequence number plus 1.If the SYN flag is clear (0), then this is the accumulated sequence number of the first data byte of this segment for the current session.
Acknowledgment number (32 bits)
If the ACK flag is set then the value of this field is the next sequence number that the sender of the ACK is expecting. This acknowledges receipt of all prior bytes (if any). The first ACK sent by each end acknowledges the other end’s initial sequence number itself, but no data.
Data offset (4 bits)
Specifies the size of the TCP header in 32-bit words. The minimum size header is 5 words and the maximum is 15 words thus giving the minimum size of 20 bytes and maximum of 60 bytes, allowing for up to 40 bytes of options in the header. This field gets its name from the fact that it is also the offset from the start of the TCP segment to the actual data.
Reserved (3 bits)
For future use and should be set to zero.
Flags (9 bits)
Contains 9 1-bit flags (control bits) as follows:
NS (1 bit): ECN-nonce - concealment protection[a]
CWR (1 bit): Congestion window reduced (CWR) flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism.[b]
ECE (1 bit): ECN-Echo has a dual role, depending on the value of the SYN flag. It indicates:
URG (1 bit): Indicates that the Urgent pointer field is significant
ACK (1 bit): Indicates that the Acknowledgment field is significant. All packets after the initial SYN packet sent by the client should have this flag set.
PSH (1 bit): Push function. Asks to push the buffered data to the receiving application.
RST (1 bit): Reset the connection
SYN (1 bit): Synchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags and fields change meaning based on this flag, and some are only valid when it is set, and others when it is clear.
FIN (1 bit): Last packet from sender
Window size (16 bits)
The size of the receive window, which specifies the number of window size units[c] that the sender of this segment is currently willing to receive.[d] (See § Flow control and § Window scaling.)
Checksum (16 bits)
The 16-bit checksum field is used for error-checking of the TCP header, the payload and an IP pseudo-header. The pseudo-header consists of the source IP address, the destination IP address, the protocol number for the TCP protocol (6) and the length of the TCP headers and payload (in bytes).
Urgent pointer (16 bits)
TCP是面向连接的协议,因此每个TCP连接都有三个阶段:
在TCP连接建立的过程中,要解决以下三个问题:
1)要使每一方能够确知对方的存在。
2)要允许双方协商一些参数
3)能够对运输实体资源进行分配
TCP把连接作为最基本的抽象,每条TCP连接有两个端点
State | Endpoint | Description |
---|---|---|
LISTEN | Server | Waiting for a connection request from any remote TCP end-point. |
SYN-SENT | Client | Waiting for a matching connection request after having sent a connection request. |
SYN-RECEIVED | Server | Waiting for a confirming connection request acknowledgment after having both received and sent a connection request. |
ESTABLISHED | Server and client | An open connection, data received can be delivered to the user. The normal state for the data transfer phase of the connection. |
SYN报文段(即建立连接的TCP请求报文段)不能携带数据,但要消耗掉一个序号
。确认报文段不能携带数据,但也要消耗掉一个序号。
客户机收到确认报文段
后,还要向服务器给出确认
,并为该TCP连接分配缓存和变量。服务器
易于受到SYN洪泛攻击。第三次握手的时候已经可以携带数据啦
例
建立/断开TCP连接过程中的报文不携带数据
交换报文的过程中,相邻的报文的seq与ack存在一定的联系.(交叉联系)🎈
但是两次相邻的握手报文中的seq却没有直接关系,它们可能独立随机生成的
可以分别将客户机和服务器发送的报文段中的seq字段记为 s e q c seq_{c} seqc和 s e q s seq_{s} seqs
类似的有 a c k c 和 a c k s ack_c和ack_s ackc和acks
在建立连接的三次握手中:
由于此阶段的报文段数据部分是空的,因此:
a c k s = s e q c + 1 a c k c = s e q s + 1 ack_s=seq_c+1 \ ack_c=seq_s+1 acks=seqc+1ackc=seqs+1
而在传输阶段规律就不是这样的,而是要结合收到的报文数据部分长度做加法
TCP传输阶段&释放连接阶段(非连接建立阶段)时,有
s e q s = a c k c s e q c = a c k s seq_s=ack_c \ seq_c=ack_s seqs=ackcseqc=acks
连接建立
参与TCP连接的两个进程中的任何一个都能终止该连接。
TCP连接释放的过程通常称为四次握手
第一步:
第二步:
第三步:若服务器已经没有要向客户机发送的数据,就通知TCP释放连接,
第四步:客户机收到连接释放报文段后,必须发出确认。
双方都各自发送一个FIN报文段 Initatoer发送的FIN作为挥手第一段 每个FIN帧都是跟随另一方的ACK段 而且Receiver发送回应Initiator的ACK段后拖住处理后续任务,接着发送自己的FIN段 |
State | Endpoint | Description |
---|---|---|
FIN-WAIT-1 | Server and client | Waiting for a connection termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent. |
FIN-WAIT-2 | Server and client | Waiting for a connection termination request from the remote TCP. |
CLOSE-WAIT | Server and client | Waiting for a connection termination request from the local user. |
CLOSING | Server and client | Waiting for a connection termination request acknowledgment from the remote TCP. |
LAST-ACK | Server and client | Waiting for an acknowledgment of the connection termination request previously sent to the remote TCP (which includes an acknowledgment of its connection termination request). |
TIME-WAIT | Server or client | Waiting for enough time to pass to be sure that all remaining packets on the connection have expired. |
CLOSED | Server and client | No connection state at all. |
1)连接建立。分为3步:
SYN= 1
,seq=x。
SYN = 1,ACK = 1
,seq=y,ack =x+1。
ACK= 1
,seq=x + 1,ack =y+1。
2)释放连接。分为4步:
FIN=1
,seq= u。
ACK= 1
,seq= v,ack = u+1。
FIN=1,ACK= 1
,seq= w,ack = u + 1。
ACK = 1
,seq u+1,ack = w+1。
Client | Server | Server | Client | |
---|---|---|---|---|
seq | u | v | w | u+1 |
ack | ♣ clubsuit ♣ | u+1 | u+1 | w+1 |
data offset
字段所占的4位(4bit)用于描述TCP报文首部的大小(4bit可以表示的最大值1111(即十进制的15));那么单位是什么?(既不是Byte,更不是bit,而是(4Byte)为单位);首部长度信息的字段
占了这个TCP报文段的首部空间
的4个bit;本文发布于:2024-02-02 01:09:16,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681218240428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |