python3 tcp黏包情况二

时间:2022-12-15 17:00:58

发送数据时间间隔很短,数据也很小,会合到一起,产生粘包

-------------------------------tcp_stickybag_server.py------------------------------- # coding:utf-8 import socket # import time  tcp_server = socket.socket() ip_port = ("127.0.0.1", 8001) tcp_server.bind(ip_port) tcp_server.listen(5) conn, addr = tcp_server.accept() from_client_msg = conn.recv(1024) print("来自客户端的消息:", from_client_msg.decode("utf-8")) # time.sleep(1) from_client_msg = conn.recv(1024) print("来自客户端的消息:", from_client_msg.decode("utf-8")) conn.close() tcp_server.close()

-------------------------------tcp_stickybag_client.py-------------------------------
# coding:utf-8
import socket
# import time

tcp_client = socket.socket()
ip_port = ("127.0.0.1", 8001)
tcp_client.connect(ip_port)
tcp_client.send("hello".encode("utf-8"))
# time.sleep(1)
tcp_client.send("world".encode("utf-8"))
tcp_client.close()