1

I am working on a program that backs up files like OneDrive or iCloud.

I made everything work but when I send a file trough the sockets it just gets stuck until I close the connection.

Here is the code sample:

The client:

def UpdateFileOnServer(file):
    print("[!] Uploading file to server: ",file)
    with open(clientFolder+file,"rb") as f:
        s.sendall(file.encode())
        file = f.read()
        s.sendall(file)
    print("[!] File has been sent.")

The server:

with open(fileLocation,"wb") as f:
    print("[!] File recieved! Downloading...")
    while True:
        data = conn.recv(4096)
        if not data:f.close();break
        f.write(data)

So to summarize again:

  • The client sends the file and says that it has been sent.
  • The server receives about 90% of the file and indefinitely hangs until I CTRL+C the client
  • When I do that the server finishes the file transfer and the file is successfully received.
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

I have found the answer to my problem thanks to all the kind commenters.

  • Create a server socket.
  • Connect with the client socket to the server socket.
  • Send the file with the client to the server.
  • When the client confirms that the file has been sent you close the connection.
  • The server continues to listen for connections.
  • Rinse and repeat!