来源于湖科大教书匠
运输层概述
AP是应用进程的缩写



运输层端口号、复用与分用的概念



UDP与TCP的对比

连接指的是逻辑连接关系,而不是物理连接

TCP使用三次握手建立连接,连接建立后,就类似于一条可靠信道

TCP不保证接收与发送各自的进程快具有对应大小的关系。
TCP是全双工通信的。
例如:发送方应用进程向发送方TCP发送了10个数据块,接收方TCP只用了4个数据块,就把收到的字节流交付给了上层的应用进程,并还原出了有意义的应用层数据



TCP的流量控制





TCP规定,即使接收窗口为0,也必须接受零窗口探测报文段、确认报文段、以及携带有紧急数据的报文段。
如果零窗口探测报文段丢失,也会打破死锁局面,因为零窗口探测报文段也有重传计时器。

TCP的拥塞控制



慢开始(slow-start)



拥塞避免(congestion-avoidance)






快重传(fast-retransmit)

快恢复(fast-recovery)



TCP超时重传时间的选择





[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GufVrtQj-1636447373575)(https://gitee.com/he-hao-hh/cloudimage/raw/master/img/image-20211030154201605.png)]


TCP可靠传输的实现








TCP的运输连接管理--------TCP的连接建立(三报文握手)

要解决的问题

实现过程
开始,TCP服务进程首先创建传输控制块,用来存储TCP连接的一些重要信息。(例如:TCP连接表等)

接下来准备接收TCP客户进程的连接请求。此时,TCP服务器进程进入监听状态,等待客户进程的连接请求。

然后,在打算建立TCP连接时,向TCP服务器进程发送TCP连接请求报文段,并进入同步已发送状态。

TCP连接请求报文段首部中的同步位SYN被设置为1(表明这是一个TCP连接请求报文段),序号字段seq被设置了一个初始值x,作为TCP客户进程所选择的初始序号。
TCP规定,SYN被设置为1的报文段不能携带数据,但要消耗掉一个序号。
由于TCP连接建立是由TCP客户主动发起的,因此称为主动打开连接。

服务器端收到请求后,若同意建立连接,则向客户端发送TCP连接请求确认报文段,并进入同步已接收状态。
该报文段的同步位SYN和确认位ACK都为1,表明这是一个TCP连接请求确认报文段。序号字段seq被设置为初始值y,作为TCP服务进程所选择的初始序号。确认号字段ack为x+1,这是对TCP客户进程所选择初始序号的确认(这个报文段也不能携带数据)

TCP客户进程收到TCP服务器的连接请求报文段后,还要发送一个普通的TCP确认报文段,并进入连接已建立状态。
确认位ACK为1,表明这是一个普通的TCP确认报文段,序号字段seq为x+1,确认号字段为y+1,这是对TCP服务器进程所选择的初始序号的确认。TCP服务器进程收到该确认报文段后也进入连接已建立状态。
TCP规定普通的TCP确认报文段可以携带数据,但如果不携带数据则不消耗序号。
在这种情况下,所发送的下一个数据报文段的序号仍是x+1

双方连接建立后,就可以进行数据传输了。

为什么不能使用“两报文握手”建立连接?

采用三报文而不用两报文是为了防止已失效的连接请求报文段突然又传送到了TCP服务器。
练习

TCP的运输连接管理--------TCP的连接释放(四报文挥手)
实现过程
客户与服务器都处于连接已建立状态,假设使用TCP客户进程的应用进程通知其主动关闭TCP连接,TCP客户进程会发送TCP连接释放报文段 并进入终止等待1状态,终止位FIN=1,确认位ACK=1表明这是一个TCP连接释放报文段,同时也对之前收到的报文段进行确认。
序号seq字段的值设置为u,它等于TCP客户进程之前已传送过的、数据的最后一个字节的序号+1。确认号ack=v,它等于TCP客户进程之前已收到的、数据的最后一个字节+1。
TCP规定终止位FIN=1的报文段即使不携带数据,也要消耗掉一个序号。

TCP服务器进程收到TCP连接释放报文段后,会发送一个普通的TCP确认报文段并进入关闭等待状态。
ACK=1,表明这是一个普通的TCP确认报文段,序号seq=v,它等于TCP服务器进程之前已经传送过的数据的最后一个字节的序号+1。
确认号ack=u+1,这是对TCP连接释放报文段的确认。
TCP服务器进程这时要通知高层应用进程:TCP客户进程要断开与自己的TCP连接。

此时TCP客户——>TCP服务器这个方向的连接就释放了。这时TCP属于半关闭状态,也就是客户进程无数据发送,但服务器进程如果还有数据要发送,TCP客户进程仍要接收。也就是说从TCP服务器——>TCP客户这个方向的连接并未关闭。(此状态会持续一段时间)
TCP客户进程收到确认报文段后就进入终止等待2状态。等待TCP服务器进程发出的TCP连接释放报文段。若使用TCP服务器进程的应用进程已没有数据要发送了,应用进程就通知其TCP服务器进程释放连接。(由于TCP连接释放是由TCP客户进程主动发起的,因此,TCP服务器进程对TCP连接的释放称为被动关闭连接)

TCP服务器进程发送TCP连接释放报文段并进入最后确认状态。
终止位FIN=1,确认位ACK=1,表明这是一个TCP连接释放报文段,同时也对之前收到的报文段进行确认。
假定序号seq字段的值为w。这是因为在半关闭状态下,TCP服务器进程可能又发送了一些数据。
确认号ack字段的值为u+1,这是对之前收到的TCP连接释放报文段的重复确认。
TCP客户进程收到TCP连接释放报文段后,必须针对该报文段发送普通的TCP确认报文段,之后进入时间等待状态。
确认位ACK=1,表明这是一个普通的TCP确认报文段,seq字段的值为u+1,这是因为TCP客户进程之前发送的TCP连接释放报文段虽然不携带数据,但要消耗掉一个序号,确认号ack=w+1,这是对所收到的TCP连接释放报文段的确认。TCP服务器进程收到该报文段后就进入关闭状态。而TCP客户进程还要进过2MSL后才能进入关闭状态。

TCP客户进程在发送完最后一个确认报文段后,为什么不直接进入关闭状态,而是进入时间等待状态?

TCP客户进程发送完最后一个TCP确认报文段后,再经过2MSL,就可以使本次连续持续时间内所产生的所有报文段从网络中消失,这样就可以使下一个新的TCP连接中,不会出现旧连接中的报文段。
TCP中保活计时器的作用

TCP报文段的首部格式

固定首部









发送窗口应该从接收窗口和拥塞窗口中取小者。
? 
TCP报文段中检错算法不是重点






扩展首部


|