黏包问题解决方案

服务端

from socket import *
import subprocess
import struct,json
server = socket(AF_INET,SOCK_STREAM)
server.setsockopt(SOL_SOCKET,SO_EXCLUSIVEADDRUSE,1)
server.bind(('127.0.0.1',8888))
server.listen(5)
while True:
    conn,addr = server.accept()
    print('收到来自%s的访问' % addr[0])
    while True:
        try:
            cmd = conn.recv(1024)
            cmd = cmd.decode("utf-8")
            ret = subprocess.Popen(cmd,shell=True,
                                   stdout=subprocess.PIPE,   #正确输出
                                   stderr=subprocess.PIPE,   #错误输出
                                   )
            stdout = ret.stdout.read()
            stderr = ret.stderr.read()
            #发送之前先创建一个报头
            total_size = len(stdout) + len(stderr)
            #报头必须是定长,不然发过去,不知道边界也没有意义
            # 先创建一个报头
            header_dict = {'total_size':total_size,
                           "md5":78978900967890878,
                           "filename":"a.txt"
                           }
            header_str = json.dumps(header_dict)
            header_bytes = header_str.encode('utf-8')
            header_size = struct.pack('i',len(header_bytes))
            conn.send(header_size)
            conn.send(header_bytes)
            conn.send(stdout)
            conn.send(stderr)
        except ConnectionResetError:
            break
    conn.close()
server.close()

客户端

from socket import *
import struct,json
client = socket()
client.connect(('127.0.0.1',8888))
while True:
    cmd = input(">>>").strip()
    if len(cmd) == 0:continue
    client.send(cmd.encode("utf-8"))

    header = client.recv(4)  #接收报头
    header_size = struct.unpack('i',header)[0] #得到报头长度
    # print(header_size)

    header_bytes = client.recv(header_size)
    header_json = header_bytes.decode('utf-8')
    header_dic = json.loads(header_json)
    print(header_dic)
    total_size = header_dic["total_size"]
    fact_size = 0
    fact = b''
    while fact_size < total_size:
        data = client.recv(1024)
        fact += data
        fact_size += len(data)
    print(fact.decode('gbk'))
client.close()

 

posted @ 2017-12-20 15:38  明王不动心  阅读(168)  评论(0编辑  收藏  举报