title: Python TCP networking inconsistencies
body: <p>I'm new to networking and I'm trying to write a simple TCP client. The first thing I have to do is to send my message length and wait for further instructions. Here's my code:</p>
  
  <pre><code>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((TCP_IP, TCP_PORT))
  
  s.send(bytearray(message_serialised_length))
  
  data = s.recv(BUFFER_SIZE)
  
  s.close()
  print("response: {}".format(struct.unpack('&gt;I', data)))
  </code></pre>
  
  <p>Sometimes, i'm able to get a response and the program continues but other times I run into this error:
  <code>struct.error: unpack requires a buffer of 4 bytes</code>.
  I'm not sure why this happens and how to fix this. What steps should I take and how can I better understand this problem? I tried removing the <code>struct</code> library and use <code>.decode()</code> instead, but that was giving me empty values.</p>