博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tcp套接字粘包解决办法
阅读量:5084 次
发布时间:2019-06-13

本文共 4213 字,大约阅读时间需要 14 分钟。

粘包只会出现在tcp,udp传输不会产生粘包现象。解决粘包的原理就是服务器预先向客户端发送客户端即将获取文件的大小。

第一版解决方案:

服务器:

1 # Author : Kelvin 2 # Date : 2019/2/2 17:38 3 from socket import * 4 import subprocess 5  6 ip_conf = ("127.0.0.1", 8888) 7 buffer_capacity = 1024 8 tcp_server = socket(AF_INET, SOCK_STREAM) 9 tcp_server.bind(ip_conf)10 tcp_server.listen(5)11 while True:12     conn, addr = tcp_server.accept()13     while True:14         try:15             cmd = conn.recv(buffer_capacity)  # 如果强制断开连接会触发try,try正是解决强制中断连接的问题16             print("收到的cmd:%s" % cmd)17             if not cmd:  # 如果使用quit断开连接,服务器会死循环收到空,该判断正是解决此问题18                 break19             res = subprocess.Popen(cmd.decode("utf8"), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,20                                    stderr=subprocess.PIPE)21             err = res.stderr.read()22             if err:23                 back_msg = err24             else:25                 back_msg = res.stdout.read()26             if not back_msg:27                 back_msg = "acute successful!".encode("gbk")28             length = len(back_msg)29             conn.send(str(length).encode("gbk"))30             re_ready = conn.recv(buffer_capacity).decode("utf8")31             if re_ready == "ready":32                 conn.send(back_msg)33         except Exception as e:34             print(e)35             break36 tcp_server.close()

客户端:

1 # Author : Kelvin 2 # Date : 2019/2/2 17:38 3 from socket import * 4  5 ip_conf = ("127.0.0.1", 8888) 6 buffer_capacity = 1024 7 tcp_client = socket(AF_INET, SOCK_STREAM) 8 tcp_client.connect(ip_conf) 9 while True:10     cmd = input("Please input cmd : ")11     if not cmd:12         continue13     if cmd == "quit":14         break15     tcp_client.send(cmd.encode("utf8"))16     re_size = int(tcp_client.recv(buffer_capacity).decode("utf-8"))17     print("大小:", re_size)18     tcp_client.send("ready".encode("utf8"))19     recved_size = 020     recved_data = b""21     while recved_size < re_size:22         recved_data += tcp_client.recv(buffer_capacity)23         recved_size = len(recved_data)24     back_msg = recved_data.decode("gbk")25     print(back_msg)26 tcp_client.close()

 

 

 

升级版:

服务器:

1 # Author : Kelvin 2 # Date : 2019/2/2 17:38 3 from socket import * 4 import subprocess 5 import struct 6  7 ip_conf = ("127.0.0.1", 8888) 8 buffer_capacity = 1024 9 tcp_server = socket(AF_INET, SOCK_STREAM)10 tcp_server.bind(ip_conf)11 tcp_server.listen(5)12 while True:13     conn, addr = tcp_server.accept()14     while True:15         try:16             cmd = conn.recv(buffer_capacity)  # 如果强制断开连接会触发try,try正是解决强制中断连接的问题17             print("收到的cmd:%s" % cmd)18             if not cmd:  # 如果使用quit断开连接,服务器会死循环收到空,该判断正是解决此问题19                 break20             res = subprocess.Popen(cmd.decode("utf8"), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,21                                    stderr=subprocess.PIPE)22             err = res.stderr.read()23             if err:24                 back_msg = err25             else:26                 back_msg = res.stdout.read()27             if not back_msg:28                 back_msg = "acute successful!".encode("gbk")29             length = len(back_msg)30             re_length = struct.pack("i", length)31             conn.send(re_length)32             conn.send(back_msg)33         except Exception as e:34             print(e)35             break36 tcp_server.close()

客户端:

1 # Author : Kelvin 2 # Date : 2019/2/2 17:38 3 from socket import * 4 import struct 5 ip_conf = ("127.0.0.1", 8888) 6 buffer_capacity = 1024 7 tcp_client = socket(AF_INET, SOCK_STREAM) 8 tcp_client.connect(ip_conf) 9 while True:10     cmd = input("Please input cmd : ")11     if not cmd:12         continue13     if cmd == "quit":14         break15     tcp_client.send(cmd.encode("utf8"))16     re_size = struct.unpack("i",tcp_client.recv(4))[0]17     print("大小:", re_size)18     recved_size = 019     recved_data = b""20     while recved_size < re_size:21         recved_data += tcp_client.recv(buffer_capacity)22         recved_size = len(recved_data)23     back_msg = recved_data.decode("gbk")24     print(back_msg)25 tcp_client.close()

 

转载于:https://www.cnblogs.com/sun-10387834/p/10348932.html

你可能感兴趣的文章
js 过滤敏感词
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
软件开发和软件测试,我该如何选择?(蜗牛学院)
查看>>
基本封装方法
查看>>
bcb ole拖拽功能的实现
查看>>
生活大爆炸之何为光速
查看>>
bzoj 2456: mode【瞎搞】
查看>>
[Typescript] Specify Exact Values with TypeScript’s Literal Types
查看>>
[GraphQL] Reuse Query Fields with GraphQL Fragments
查看>>
Illustrated C#学习笔记(一)
查看>>
理解oracle中连接和会话
查看>>
两种最常用的Sticky footer布局方式
查看>>
Scrapy实战篇(三)之爬取豆瓣电影短评
查看>>
HDU 5510 Bazinga KMP
查看>>
[13年迁移]Firefox下margin-top问题
查看>>
Zookeeper常用命令 (转)
查看>>
Java程序IP v6与IP v4的设置
查看>>
RUP(Rational Unified Process),统一软件开发过程
查看>>
数据库链路创建方法
查看>>
Enterprise Library - Data Access Application Block 6.0.1304
查看>>