学习笔记(10):Python网络编程&并发编程-粘包现象

立即学习:https://edu.csdn.net/course/play/24458/296240?utm_source=blogtoedu

       粘包现象:服务器接收到客户端的命令后,进行执行得到结果后,再发送回给客户端,在这个过程中如果服务器返回的结果的字节数会大于客户端所接收最大字节数(此处以1024为客户端接收的最大字节数),则大于1024字节的残余结果会堆积在服务器发送返回客户端的管道中,这个现象叫做残余数据等下一次再执行客户端命令返回命令结果给客户端时,会先把管道中的残余数据发送返回给客户端,这时候就会出现客户端接收的结果并不是自己发送命令所希望得到的结果,因为所接收的结果含有上一次执行时的残余数据甚至只有残余数据,即多次命令(也成为多个包)的结果掺杂在了一起,这种现象成为粘包现象。例子如下:

'''
        先运行服务器端,再在客户端上一次输入dir、tasklist、dir命令,观察服务器端执行命令的结果的字节数长度,以及观察客户端接收到的结果,尤其是两次运行dir命令的结果
'''


客户端的结果:

'''
E:\python3\venv2\venv\Scripts\python.exe C:/Users/jinlin/Desktop/python_further_study/socket编程/简单的套接字通讯加循环/客户端.py
请输入命名:dir
服务器返回来的数据:  驱动器 C 中的卷是 本地磁盘
 卷的序列号是 B476-3C7C

 C:\Users\jinlin\Desktop\python_further_study\socket编程\模拟ssh远程操作命令 的目录

2020/03/09  10:57    <DIR>          .
2020/03/09  10:57    <DIR>          ..
2020/03/07  13:35                 0 __init__.py
2020/03/07  10:02               895 客户端_.py
2020/03/09  10:57             1,112 服务器端_.py
               3 个文件          2,007 字节
               2 个目录 124,751,876,096 可用字节

**************************************************
请输入命名:tasklist
服务器返回来的数据: 
映像名称                       PID 会话名              会话#       内存使用 
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0          4 K
System                           4 Services                   0        588 K
smss.exe                       324 Services                   0        804 K
csrss.exe                      524 Services                   0      9,064 K
csrss.exe                      620 Console                    1     32,996 K
wininit.exe                    628 Services                   0      4,144 K
winlogon.exe                   656 Console                    1      6,692 K
services.exe                   724 Services                   0      8,568 K
lsass.exe                      732 Services                   0     11,756 K
svchost.exe                    804 Services                   0     11,556 K
svchost.exe                    844 Services                   0      9,336 K
dwm.exe 
**************************************************
请输入命名:dir
服务器返回来的数据:                        948 Console                    1     24,656 K
nvvsvc.exe                     956 Services                   0      2,648 K
nvxdsync.exe                  1000 Console                    1      7,264 K
nvvsvc.exe                    1008 Console                    1      1,684 K
svchost.exe                    276 Services                   0     31,996 K
svchost.exe                    392 Services                   0     48,924 K
svchost.exe                    412 Services                   0     25,584 K
svchost.exe                    736 Services                   0     49,284 K
RtkAudioService64.exe         1056 Services                   0          4 K
RAVBg64.exe                   1076 Console                    1      1,596 K
RAVBg64.exe                   1084 Console                    1      1,432 K
ZhuDongFangYu.exe             1128 Services                   0      4,528 K
svchost.exe                   1280 Services                   0     17,032 K
spoolsv.exe       
**************************************************

'''


服务器端的结果:
'''
464
**************************************************
9674
**************************************************
464
**************************************************

'''

       由上可知,dir命令返回的结果字节数是496,由tasklist运行的结果字节数为9766个,因此两次的dir命令执行的结果不一样,因为第二次dir命令的结果与tasklist命令的结果混在了一起,产生了粘包现象。

简单的粘包原因如下图:

发布了39 篇原创文章 · 获赞 11 · 访问量 424

猜你喜欢

转载自blog.csdn.net/qq_45769063/article/details/104748655