title: TCP transfer files without closing socket
body: <p>I coded a simple TCP server - client file sharing but it only work when the script close the socket in the client side.</p>
  <p>I tried to remove the s.close inside the client python script and it gave me an error. Any idea?
  I dont know if it even possible to do this kind of things with TCP without closing socket.</p>
  <p>here is my code</p>
  <p>server.py</p>
  <pre><code>import socket
  import tqdm
  import os
  
  SERVER_HOST = &quot;192.168.1.48&quot;
  SERVER_PORT = 5001
  
  BUFFER_SIZE = 4096
  SEPARATOR = &quot;&lt;SEPARATOR&gt;&quot;
  
  s = socket.socket()
  s.bind((SERVER_HOST, SERVER_PORT))
  
  s.listen(5)
  print(f&quot;[*] Listening as {SERVER_HOST}:{SERVER_PORT}&quot;)
  
  client_socket, address = s.accept()
  
  print(f&quot;[+] {address} is connected.&quot;)
  
  
  def receive_file(filename):
  
      received = client_socket.recv(BUFFER_SIZE).decode()
      filename, filesize = received.split(SEPARATOR)
   
      filename = os.path.basename(filename)
    
      filesize = int(filesize)
  
      progress = tqdm.tqdm(range(filesize), f&quot;Receiving {filename}&quot;, unit=&quot;B&quot;, unit_scale=True, unit_divisor=1024)
      with open(filename, &quot;wb&quot;) as f:
          while True:
              # read 1024 bytes from the socket (receive)
              bytes_read = client_socket.recv(BUFFER_SIZE)
              if not bytes_read:
                  # nothing is received
                  # file transmitting is done
                  break
              # write to the file the bytes we just received
              f.write(bytes_read)
              # update the progress bar
              progress.update(len(bytes_read))
  
  while True:
      command = input(&quot;--&gt;&quot;)
      client_socket.send(str.encode(command))
      filename = command[9:]
      receive_file(filename)
      
  
  </code></pre>
  <p>client.py</p>
  <pre><code>import socket
  import tqdm
  import os
  
  SEPARATOR = &quot;&lt;SEPARATOR&gt;&quot;
  BUFFER_SIZE = 4096 # send 4096 bytes each time step
  
  
  
  host = &quot;192.168.1.48&quot;
  
  port = 5001
  
  filesize = os.path.getsize(filename)
  s = socket.socket()
  
  print(f&quot;[+] Connecting to {host}:{port}&quot;)
  s.connect((host, port))
  print(&quot;[+] Connected.&quot;)
  
  def send_file(filename,filesize):
  
      s.send(f&quot;{filename}{SEPARATOR}{filesize}&quot;.encode())
  
     
      progress = tqdm.tqdm(range(filesize), f&quot;Sending {filename}&quot;, unit=&quot;B&quot;, unit_scale=True, unit_divisor=1024)
      with open(filename, &quot;rb&quot;) as f:
          while True:
              
              bytes_read = f.read(BUFFER_SIZE)
              if not bytes_read:
                  # file transmitting is done
                  break
            
              s.sendall(bytes_read)
         
              progress.update(len(bytes_read))
      # close the socket
      s.close()
  
  
  while True:
      command = s.recv(4096)
      if command[:8].decode(&quot;utf-8&quot;) == &quot;download&quot;:
          print(&quot;yes it start&quot;)
          filename = command[9:].decode(&quot;utf-8&quot;)
          print(filename)
          filesize = os.path.getsize(filename)
          send_file(filename,filesize)
  
  </code></pre>