pythonfilesocketsbinarytransfer

I'm trying to send multiple files over socket, and the server is getting stuck after a file transfer


Why, I have no idea, but the server is getting stuck on bytes_read = connection.recv(BUFFER) after the file transfer.

So you know BUFFER is 4096.

client.py

                with open(filename, "rb") as file:
                    while True:
                        bytes_read = file.read(BUFFER)
                        if bytes_read == b'':
                            client.send(b'<EOF>')
                            break

                        client.sendall(bytes_read)
                        progress.update(len(bytes_read))
                progress.close()
                client.recv(BUFFER).decode()

server.py

                with open(filename, 'wb') as file:
                    while True:
                        bytes_read = connection.recv(BUFFER)
                        if bytes_read == b'<EOF>':
                            break
                            
                        file.write(bytes_read)
                        progress.update(len(bytes_read))
                    progress.close()
                    connection.send('release'.encode())

When I walk through the code everything works fine, but when I run the code the server gets stuck on bytes_read = connection.recv(BUFFER) and never sees the bytes_read == b'<EOF>'.

Could it be that the packet being sent is to small?


Solution

  • I've found that checking the file size in a while loop will work.

    client.py

                    with open(filename, "rb") as file: 
                         while True: 
                             _bytes = file.read(BUFFER) 
                             if _bytes == b'': 
                                 progress.update(len(_bytes)) 
                                 break 
      
                             client.sendall(_bytes) 
                             progress.update(len(_bytes)) 
                     client.recv(BUFFER).decode()
    

    server.py

                    file_data = b'' 
                     with open(filename, 'wb') as file: 
                         while len(file_data) < filesize: 
                             _bytes = connection.recv(BUFFER) 
                             file_data += _bytes 
                             file.write(file_data) 
                             progress.update(len(_bytes))