来源于湖科大教书匠
运输层概述
AP是应用进程的缩写
![image-20211027190921892](https://img-blog.csdnimg.cn/img_convert/3024ef7fbcbf2b8a9274a7fafe2ce0c8.png)
![image-20211027190644247](https://img-blog.csdnimg.cn/img_convert/2dbf1f5217a9c12d10a842e39fecd62e.png)
![image-20211027190902870](https://img-blog.csdnimg.cn/img_convert/83afb45745a903ed927b343e605ae53a.png)
运输层端口号、复用与分用的概念
![image-20211027212126184](https://img-blog.csdnimg.cn/img_convert/1b0aac6a3c25d44a4e1c39eddbc9f76d.png)
![image-20211027212212761](https://img-blog.csdnimg.cn/img_convert/63d042195fd231069db6ba4ef2f39290.png)
![image-20211027212243552](https://img-blog.csdnimg.cn/img_convert/29be7d07edab86db4d365e02c8f5390d.png)
UDP与TCP的对比
![image-20211027214043133](https://img-blog.csdnimg.cn/img_convert/7fd0b25334aec421b0b7ef472ce9b426.png)
连接指的是逻辑连接关系,而不是物理连接
![image-20211027214214773](https://img-blog.csdnimg.cn/img_convert/d68734e28102ee780e27400f1f6c3272.png)
TCP使用三次握手建立连接,连接建立后,就类似于一条可靠信道
![image-20211027214749566](https://img-blog.csdnimg.cn/img_convert/c8ad5c4cda7899c9ead40998be7afcd9.png)
TCP不保证接收与发送各自的进程快具有对应大小的关系。
TCP是全双工通信的。
例如:发送方应用进程向发送方TCP发送了10个数据块,接收方TCP只用了4个数据块,就把收到的字节流交付给了上层的应用进程,并还原出了有意义的应用层数据
![image-20211027214836361](https://img-blog.csdnimg.cn/img_convert/d514f4de7c5eeba86b62f6d9c255cb1b.png)
![image-20211027214916504](https://img-blog.csdnimg.cn/img_convert/dbf6697789a3a5e47d27940c75434327.png)
![image-20211027214930166](https://img-blog.csdnimg.cn/img_convert/e66d2a5e9eb63086bcb450ccc568327f.png)
TCP的流量控制
![image-20211029214253372](https://img-blog.csdnimg.cn/img_convert/9a7613ce595b52c440805dd913616fd5.png)
![image-20211029213530247](https://img-blog.csdnimg.cn/img_convert/c9621568ea1c9af75c188c4289e8d6f4.png)
![image-20211029213722363](https://img-blog.csdnimg.cn/img_convert/2e47718702d611bbbf79404c8ed29c30.png)
![image-20211029213827830](https://img-blog.csdnimg.cn/img_convert/1fcb1c848b9ec6b3930d9efe29ebca0b.png)
![image-20211029213900935](https://img-blog.csdnimg.cn/img_convert/c2d6d4011c5e8f09cb622f4d0a4dc820.png)
TCP规定,即使接收窗口为0,也必须接受零窗口探测报文段、确认报文段、以及携带有紧急数据的报文段。
如果零窗口探测报文段丢失,也会打破死锁局面,因为零窗口探测报文段也有重传计时器。
![image-20211029214223450](https://img-blog.csdnimg.cn/img_convert/ff74324ea34f30b8084bbeee130e2033.png)
TCP的拥塞控制
![image-20211030095201537](https://img-blog.csdnimg.cn/img_convert/a02661e72e092c02fde242f72d8feac1.png)
![image-20211030095235128](https://img-blog.csdnimg.cn/img_convert/2e8f61aedf18e9f7802fac6221b5bbe6.png)
![image-20211030095312211](https://img-blog.csdnimg.cn/img_convert/da24d9f2d06700ea9bcd0e92916aafa8.png)
慢开始(slow-start)
![image-20211030095539709](https://img-blog.csdnimg.cn/img_convert/eeb178d5810ba00214b4214f5bbf2f29.png)
![image-20211030095610579](https://img-blog.csdnimg.cn/img_convert/eb34611dac7428f1f349d56d368b0dea.png)
![image-20211030095634372](https://img-blog.csdnimg.cn/img_convert/ea9fcca30afe970354974f3f6a45c24f.png)
拥塞避免(congestion-avoidance)
![image-20211030095800158](https://img-blog.csdnimg.cn/img_convert/a6efe8772eb4b14ff4f98a45c2107b67.png)
![image-20211030095829209](https://img-blog.csdnimg.cn/img_convert/1ce6a0281c37dd97557a64febcbbbe75.png)
![image-20211030095852757](https://img-blog.csdnimg.cn/img_convert/86b6dabf90b538d448f4befa59af49da.png)
![image-20211030095928426](https://img-blog.csdnimg.cn/img_convert/acf211a1012292d70f959b471f23c159.png)
![image-20211030100019450](https://img-blog.csdnimg.cn/img_convert/54c5bf5ce8db2eddc54e706da299979c.png)
![image-20211030100131718](https://img-blog.csdnimg.cn/img_convert/4d73252acfac44db76e10cb9e3ec1269.png)
快重传(fast-retransmit)
![image-20211030100222643](https://img-blog.csdnimg.cn/img_convert/a5bf34d6c009ebe400f51cfed0063faf.png)
快恢复(fast-recovery)
![image-20211030100439832](https://img-blog.csdnimg.cn/img_convert/374f3ec2a87bc2377d9f79dce1345143.png)
![image-20211030100636651](https://img-blog.csdnimg.cn/img_convert/ea0aae78fb480ae3214b40a9cb42fc48.png)
![image-20211030100809302](https://img-blog.csdnimg.cn/img_convert/9b6c9655c8e188be4aa84593a1026212.png)
TCP超时重传时间的选择
![image-20211030153605417](https://img-blog.csdnimg.cn/img_convert/a6b2bc1cb5c04b89375497f34d2d5ae3.png)
![image-20211030153620914](https://img-blog.csdnimg.cn/img_convert/fb5ad97342fbe2d3903ea17a0e8a2d91.png)
![image-20211030153707043](https://img-blog.csdnimg.cn/img_convert/f3d62ee4e25dddab161f96b4b513ebf5.png)
![image-20211030153749198](https://img-blog.csdnimg.cn/img_convert/5495f81f5b79c6d809ab4d11d3c47fcd.png)
![image-20211030154042318](https://img-blog.csdnimg.cn/img_convert/e1834f14d8d997393b15eb11a9e2f518.png)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GufVrtQj-1636447373575)(https://gitee.com/he-hao-hh/cloudimage/raw/master/img/image-20211030154201605.png)]
![image-20211030154224175](https://img-blog.csdnimg.cn/img_convert/a93fdc9f587f295199f1679a22413e5a.png)
![image-20211030154540440](https://img-blog.csdnimg.cn/img_convert/91b08c7520fcf3e1a1766cbc1369cf9d.png)
TCP可靠传输的实现
![image-20211030162846743](https://img-blog.csdnimg.cn/img_convert/467916e943e870164b2cf68a11ec22d5.png)
![image-20211030162910785](https://img-blog.csdnimg.cn/img_convert/6b29f17f83301624055988abe2b9d4ac.png)
![image-20211030163049484](https://img-blog.csdnimg.cn/img_convert/e99807fee65c7edb0806078de236446f.png)
![image-20211030163121889](https://img-blog.csdnimg.cn/img_convert/0a8c69d28124cc2042deb2517a3e42c1.png)
![image-20211030163324139](https://img-blog.csdnimg.cn/img_convert/71b3cd44cb4054197124e3cc5710b772.png)
![image-20211030163339222](https://img-blog.csdnimg.cn/img_convert/e7862e30f94a8281f0c7ece5382c418a.png)
![image-20211030163407408](https://img-blog.csdnimg.cn/img_convert/5776c0507201891c3811c91648c21306.png)
![image-20211030163432275](https://img-blog.csdnimg.cn/img_convert/3a2868f9180499ab0e15fa123f43f923.png)
TCP的运输连接管理--------TCP的连接建立(三报文握手)
![image-20211031100601758](https://img-blog.csdnimg.cn/img_convert/a03730fa41b212518f6e1b2bf2024ff3.png)
要解决的问题
![image-20211031100705652](https://img-blog.csdnimg.cn/img_convert/672c47dac34e202f09c3015a226503c3.png)
实现过程
开始,TCP服务进程首先创建传输控制块,用来存储TCP连接的一些重要信息。(例如:TCP连接表等)
![image-20211031101335226](https://img-blog.csdnimg.cn/img_convert/5eef0b0f13104620e11d0895bb227ef6.png)
接下来准备接收TCP客户进程的连接请求。此时,TCP服务器进程进入监听状态,等待客户进程的连接请求。
![image-20211031101313486](https://img-blog.csdnimg.cn/img_convert/b1e4d10c127c3c8fe45f4dd300494d1b.png)
然后,在打算建立TCP连接时,向TCP服务器进程发送TCP连接请求报文段,并进入同步已发送状态。
![image-20211031101413813](https://img-blog.csdnimg.cn/img_convert/fba0c0e5e5f3391896f14a51268dde9d.png)
TCP连接请求报文段首部中的同步位SYN被设置为1(表明这是一个TCP连接请求报文段),序号字段seq被设置了一个初始值x,作为TCP客户进程所选择的初始序号。
TCP规定,SYN被设置为1的报文段不能携带数据,但要消耗掉一个序号。
由于TCP连接建立是由TCP客户主动发起的,因此称为主动打开连接。
![image-20211031101929916](https://img-blog.csdnimg.cn/img_convert/c896c77985b620e8b49a2ce9f6ac7675.png)
服务器端收到请求后,若同意建立连接,则向客户端发送TCP连接请求确认报文段,并进入同步已接收状态。
该报文段的同步位SYN和确认位ACK都为1,表明这是一个TCP连接请求确认报文段。序号字段seq被设置为初始值y,作为TCP服务进程所选择的初始序号。确认号字段ack为x+1,这是对TCP客户进程所选择初始序号的确认(这个报文段也不能携带数据)
![image-20211031102515271](https://img-blog.csdnimg.cn/img_convert/5ab1241e2aa767788ed3da0b55b8761f.png)
TCP客户进程收到TCP服务器的连接请求报文段后,还要发送一个普通的TCP确认报文段,并进入连接已建立状态。
确认位ACK为1,表明这是一个普通的TCP确认报文段,序号字段seq为x+1,确认号字段为y+1,这是对TCP服务器进程所选择的初始序号的确认。TCP服务器进程收到该确认报文段后也进入连接已建立状态。
TCP规定普通的TCP确认报文段可以携带数据,但如果不携带数据则不消耗序号。
在这种情况下,所发送的下一个数据报文段的序号仍是x+1
![image-20211031103640203](https://img-blog.csdnimg.cn/img_convert/36feae24c1e0dcc8bf7b3ee5f4b89883.png)
双方连接建立后,就可以进行数据传输了。
![image-20211031103722095](https://img-blog.csdnimg.cn/img_convert/834c79fd035115473e6c1c08c9519c54.png)
为什么不能使用“两报文握手”建立连接?
![image-20211031103859163](https://img-blog.csdnimg.cn/img_convert/30b536090b7e2dbfb7e9b7431a2eeeb3.png)
采用三报文而不用两报文是为了防止已失效的连接请求报文段突然又传送到了TCP服务器。
练习
![image-20211031104550184](https://img-blog.csdnimg.cn/img_convert/8fc974ea59ad2605a6bda413d80e5c87.png)
TCP的运输连接管理--------TCP的连接释放(四报文挥手)
实现过程
客户与服务器都处于连接已建立状态,假设使用TCP客户进程的应用进程通知其主动关闭TCP连接,TCP客户进程会发送TCP连接释放报文段 并进入终止等待1状态,终止位FIN=1,确认位ACK=1表明这是一个TCP连接释放报文段,同时也对之前收到的报文段进行确认。
序号seq字段的值设置为u,它等于TCP客户进程之前已传送过的、数据的最后一个字节的序号+1。确认号ack=v,它等于TCP客户进程之前已收到的、数据的最后一个字节+1。
TCP规定终止位FIN=1的报文段即使不携带数据,也要消耗掉一个序号。
![image-20211031121136733](https://img-blog.csdnimg.cn/img_convert/a10a7ef0c3d0b0fa8245d8b8485d9c24.png)
TCP服务器进程收到TCP连接释放报文段后,会发送一个普通的TCP确认报文段并进入关闭等待状态。
ACK=1,表明这是一个普通的TCP确认报文段,序号seq=v,它等于TCP服务器进程之前已经传送过的数据的最后一个字节的序号+1。
确认号ack=u+1,这是对TCP连接释放报文段的确认。
TCP服务器进程这时要通知高层应用进程:TCP客户进程要断开与自己的TCP连接。
![image-20211031121805402](https://img-blog.csdnimg.cn/img_convert/175e204654bb9e4579b2dd65a4cc7211.png)
此时TCP客户——>TCP服务器这个方向的连接就释放了。这时TCP属于半关闭状态,也就是客户进程无数据发送,但服务器进程如果还有数据要发送,TCP客户进程仍要接收。也就是说从TCP服务器——>TCP客户这个方向的连接并未关闭。(此状态会持续一段时间)
TCP客户进程收到确认报文段后就进入终止等待2状态。等待TCP服务器进程发出的TCP连接释放报文段。若使用TCP服务器进程的应用进程已没有数据要发送了,应用进程就通知其TCP服务器进程释放连接。(由于TCP连接释放是由TCP客户进程主动发起的,因此,TCP服务器进程对TCP连接的释放称为被动关闭连接)
![image-20211031122734273](https://img-blog.csdnimg.cn/img_convert/0756c54f3c1cd712bd8a3db489140ab2.png)
TCP服务器进程发送TCP连接释放报文段并进入最后确认状态。
终止位FIN=1,确认位ACK=1,表明这是一个TCP连接释放报文段,同时也对之前收到的报文段进行确认。
假定序号seq字段的值为w。这是因为在半关闭状态下,TCP服务器进程可能又发送了一些数据。
确认号ack字段的值为u+1,这是对之前收到的TCP连接释放报文段的重复确认。![image-20211031123917392](https://img-blog.csdnimg.cn/img_convert/c0c64c694c382ef5ed63c1b54ba67ba1.png)
TCP客户进程收到TCP连接释放报文段后,必须针对该报文段发送普通的TCP确认报文段,之后进入时间等待状态。
确认位ACK=1,表明这是一个普通的TCP确认报文段,seq字段的值为u+1,这是因为TCP客户进程之前发送的TCP连接释放报文段虽然不携带数据,但要消耗掉一个序号,确认号ack=w+1,这是对所收到的TCP连接释放报文段的确认。TCP服务器进程收到该报文段后就进入关闭状态。而TCP客户进程还要进过2MSL后才能进入关闭状态。
![image-20211031131703027](https://img-blog.csdnimg.cn/img_convert/f22aa935bd73ac4b38018c00b1123994.png)
TCP客户进程在发送完最后一个确认报文段后,为什么不直接进入关闭状态,而是进入时间等待状态?
![image-20211031132020505](https://img-blog.csdnimg.cn/img_convert/c4a8c442784e671796b0ac2338238cf4.png)
TCP客户进程发送完最后一个TCP确认报文段后,再经过2MSL,就可以使本次连续持续时间内所产生的所有报文段从网络中消失,这样就可以使下一个新的TCP连接中,不会出现旧连接中的报文段。
TCP中保活计时器的作用
![image-20211031132503638](https://img-blog.csdnimg.cn/img_convert/473c8ff8b5f7595b83355e69b9f5d09c.png)
TCP报文段的首部格式
![image-20211103111030967](https://img-blog.csdnimg.cn/img_convert/dae143495dd102eb93b0d812e19f6c21.png)
固定首部
![image-20211103111104172](https://img-blog.csdnimg.cn/img_convert/7aed7bd5e50e9621b261c751be21ce17.png)
![image-20211103111201328](https://img-blog.csdnimg.cn/img_convert/8de519deb677b9fbebc37301fb443b41.png)
![image-20211103111230658](https://img-blog.csdnimg.cn/img_convert/21d110795f2217f1cf553423559ba6a5.png)
![image-20211103111245092](https://img-blog.csdnimg.cn/img_convert/c079a70c43c58bd97ba7301ddf3ff248.png)
![image-20211103111300040](https://img-blog.csdnimg.cn/img_convert/512d1fe75408cb3736c4b49006de465f.png)
![image-20211103111356589](https://img-blog.csdnimg.cn/img_convert/f4510d07b85b3cc8e51876ff3f84db4a.png)
![image-20211103111416113](https://img-blog.csdnimg.cn/img_convert/0f03b9cb222b44444870b366300e7664.png)
![image-20211103111431560](https://img-blog.csdnimg.cn/img_convert/ac7f49c2498058fae63de376d33634c9.png)
![image-20211103111444206](https://img-blog.csdnimg.cn/img_convert/bbd4f3e9b7984a74c7a1a27d3a5a3047.png)
发送窗口应该从接收窗口和拥塞窗口中取小者。
? ![image-20211103111602685](https://img-blog.csdnimg.cn/img_convert/90097fc9180ffea4f13f464f1f4deb3c.png)
TCP报文段中检错算法不是重点
![image-20211103111624880](https://img-blog.csdnimg.cn/img_convert/c598b539a4aa96dfd42a0908826a31a2.png)
![image-20211103111729993](https://img-blog.csdnimg.cn/img_convert/f00e9a1a9be56e1613f156a18ea8d8f3.png)
![image-20211103111750342](https://img-blog.csdnimg.cn/img_convert/b163fa42ccb7898a432e682737166e7a.png)
![image-20211103111800805](https://img-blog.csdnimg.cn/img_convert/bee108469c6ec6849889b8eaafef7d6c.png)
![image-20211103111816279](https://img-blog.csdnimg.cn/img_convert/abfe9e7f628aa8e88a3afb7be26b26ea.png)
![image-20211103111838925](https://img-blog.csdnimg.cn/img_convert/ad751daf9ce5271f2a8a4d9f1aab0caf.png)
扩展首部
![image-20211103111907475](https://img-blog.csdnimg.cn/img_convert/7fb529bc95aca94f855fa09d7abe6acd.png)
![image-20211103111925990](https://img-blog.csdnimg.cn/img_convert/c5f52d2503158b472f59e82e71868779.png)
|