码迷,mamicode.com
首页 > 其他好文 > 详细

socket 粘包

时间:2018-11-18 19:26:18      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:div   内容   none   读取   粘包   soc   获取   splay   ocx   

---恢复内容开始---

socket粘包:两个包连在一起了

解决办法,在两个包之间的信息接收一个返回信息

技术分享图片
import socket
import os
# import time
server = socket.socket()
server.bind((localhost,6969))
server.listen()

while True:
    conn,addr = server.accept()
    print("new conn :",addr)
    while True:
        data = conn.recv(1024)
        if not data:
            print("客户端已经断开")
            break
        print("执行指令:",data)
        cmd_res = os.popen(data.decode(utf-8)).read()
        if len(cmd_res) == 0:
            cmd_res = "cmd has no output,,,"

        conn.send(str(len(cmd_res.encode(utf-8))).encode(utf-8))
        # time.sleep(0.5)
        ‘‘‘-----------------------------------‘‘‘
        client_ack = conn.recv(1024) # wait client to continue
        ‘‘‘-----------------------------------‘‘‘
        print("client_ack:",client_ack.decode())
        conn.send(cmd_res.encode(utf-8))
        print("send done")

server.close()
server
技术分享图片
#客户端
import socket

client = socket.socket()
client.connect((localhost,6969))

while True:
    cmd = input(">>:").strip()
    if len(cmd) == 0: continue
    client.send(cmd.encode(utf-8))
    cmd_res_size = client.recv(1024)
    print(命令结果大小:,cmd_res_size.decode())
    ‘‘‘-------------------‘‘‘
    client.send("准备好啦".encode("utf-8"))
    ‘‘‘-------------------‘‘‘
    size = 0
    receive_data = b‘‘
    while size < int(cmd_res_size.decode()):
        data = client.recv(1024)
        size += len(data)
        receive_data += data
    else:
        print(receive_data.decode())
        print("cmd res receive donr,,,,",size)

client.close()
client

 

 

ftp server

1.读取文件名  2.检测文件是否存在 3.打开文件 4.检测文件大小 5.发送文件大小和md5给客户端

6.接受客户端确认信息(防止粘包) 7.开始边读边发

---恢复内容结束---

技术分享图片
import socket,os
import hashlib
server = socket.socket()
server.bind((localhost,6969))
server.listen()

while True:
    conn,addr = server.accept()
    print("new conn :",addr)
    while True:
        data = conn.recv(1024)
        if not data:
            print("客户端已经断开")
            break
        cmd,filename = data.decode().split()
        print(filename)
        if os.path.isfile(filename):
            f = open(filename,rb)
            m = hashlib.md5()
            file_size = os.stat(filename).st_size   # 获取文件大小
            conn.send(str(file_size).encode(utf-8))
            conn.recv(1024) # wait client ack
            for line in f:
                m.update(line)
                conn.send(line)
            print("file md5:",m.hexdigest())
            f.close()
            conn.send(m.hexdigest().encode())
        print("send done")

server.close()
ftp_server
技术分享图片
import socket
import hashlib

client = socket.socket()
client.connect((localhost,6969))

while True:
    cmd = input(">>:").strip()
    if len(cmd) == 0: continue
    if cmd.startswith("get"):
        client.send(cmd.encode(utf-8))
        server_response = client.recv(1024)
        print("server_response:",server_response)
        client.send(b"ready to recv file")
        file_total_size = int(server_response.decode())
        received_size = 0
        filename = cmd.split()[1]
        f = open("new.docx","wb")
        m = hashlib.md5()
        while received_size < file_total_size:
            if file_total_size - received_size >= 1024:
                size = 1024
            else:   # 最后一次,剩下多少收多少
                size = file_total_size - received_size
                print("--------",size)      # 防止服务器两次send 粘包
            data = client.recv(size)
            received_size += len(data)
            m.update(data)
            f.write(data)
            print(file_total_size,received_size)
        else:
            new_file_md5 = m.hexdigest()
            print("file recv done")
            f.close()
        server_file_md5 = client.recv(1024)
        print("server file md5:",server_file_md5)
        print("client file md5:",new_file_md5)

client.close()
ftp_client

 

 

 

 

.

socket 粘包

标签:div   内容   none   读取   粘包   soc   获取   splay   ocx   

原文地址:https://www.cnblogs.com/yfjly/p/9978746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!