0
votes

I have been working on an image transfer server and I've run into a problem with saving multiple images. The server can only save multiple images (from more than one client) if the socket on the client end closes which only allows that client to send one image. I've also tried to send the filesize and stop saving when the filesize is met, but the image then becomes impossible to open for whatever reason.

Client Code

f = open(filename, "rb")
while True:
    data = f.read(512)
    if not data:
        break
    s.send(data)
f.close()

Server Code

for client in clients:
    while True:
        data = client[0].recv(512)
        if data == "":
           break
        f.write(data)
1
Please provide a minimal reproducible example. That would help to answer: How did you encode the image file size? How did you send the file size? How did you receive it? You might also see this answer for some related tips: stackoverflow.com/q/45620230/1076479 - Gil Hamilton

1 Answers

-1
votes

Try to send an empty string from the client after the transfer of the image is completed. I believe that the problem lies in the fact that the server does not distinguish how many images are transmitted.

Client:

for file in files:
    with open(filename, "rb") as f:
        data = f.read()
        while data:
            s.send(data[:512])
            data = data[512:]
        s.send('')

Server:

from functools import partial

while True:

    with open(filename, "rb") as f:
        data = b"".join(iter(partial(client[0].recv, 512), b''))
        f.write(data)