简体   繁体   中英

Sending large image file's binary data over TCP sockets

I'm trying to send an image's binary data from the server (after I opened the image file as 'rb') to the client:

This is the server-side : sending the image binary data in pieces to the client:

BUFSIZ = 262144 #max tcp length

while len(pic_data) > BUFSIZ:
    client.send(pic_data[:BUFSIZ])
    print len(pic_data[:BUFSIZ])
    pic_data = pic_data[BUFSIZ:]
if len(pic_data) <= BUFSIZ:
    client.send(pic_data)
    print len(pic_data)
client.send("alldone") #telling the client he finished

The client-side is receiving the info in pieces and saving the whole binary data as a file on the computer:

pic_data = ""
r = client_socket.recv(BUFSIZ)
while r != "alldone":
    pic_data += r
    r = client_socket.recv(BUFSIZ)
    print len(r)
    print str(questions_data[count][0])
 print len(pic_data)

if r == "alldone":
    filepath = "C:\Users\hilab\PycharmProjects\dafyProject\\pic" + str(questions_data[count][0]) + 
    ".jpg"
    with open(filepath, 'wb') as file:
        file.write(pic_data)
  • questions_data[count][0] grows +1 for every image that's sent

However, if an image's binary-data length is more than BUFSIZ (262144), the image isn't successfully saved and the program gets stuck.

I suspect that somehow the 'alldone' message is added to the pic_data variable (cause I've noticed that if I change the final message from 'alldone' to a message with different length - the length of the last piece of the image binary-data that's accepted by the client changes as well)

Do you know what the problem is? how can I fix it?

messages sent using tcp might be added or split in the sending process. What I would do is add to each message its length at the beginning (8 digits will defiantly do if you want to be safe, it must be constant though). On the client-side, you will have to first receive a message in the length of 8, interpret the size of the incoming message, and then use the recv() function with the length you just read. That way you can make sure each message is received fully and wasn't added to another message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM