title: Writing into a TCP socket from C to Python
body: <p>I've written a TCP socket in C that connects to port <code>5678</code>. It is supposed to transmit a String from C to a TCP client written in Python.</p>
  
  <p>Here's the server loop written in C:</p>
  
  <pre><code>for(;;) {
      bzero(buffer, 256);
  
      // Socket read functions
      n = read(clientsockfd, buffer, 255);
  
      if (n == 0) {
          printf("[Socket]: Client disconnected\n");
          break;
      }
  
      if (n &lt; 0) {
          error("[Socket]: Error: Cannot read from socket\n");
      }
  
      // Socket write functions
      printf("[Socket]: Sent: %s\n", "Some message!");
      n = write(clientsockfd, "Some message!", 0);
  
      if (n &lt; 0) {
          error("[Socket]: Error: Cannot write to socket\n");
      }
  }
  </code></pre>
  
  <p>My Python client connects to the TCP socket:</p>
  
  <pre><code>try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect(("localhost", 5678))
  except socket.error: # Connection refused error
      logging.critical("Could not connect to the socket!")
  </code></pre>
  
  <p>Then the client enters an infinite loop requesting data, receiving data, and printing the data:</p>
  
  <pre><code>while True:
      sock.send(str(0).encode('utf-8'))
      print(sock.recv(1024).decode('utf-8')))
  </code></pre>
  
  <p>But the Python client hangs on the <code>recv()</code> method.</p>
  
  <p>I imagine this is a problem with the C server since I previously had a server written in Python and the client worked just fine.</p>
  
  <p>Also, when debugging in <code>telnet</code>, the server has the expected behavior:</p>
  
  <pre><code>telnet localhost 5678
  </code></pre>
  
  <p>Any help with this issue would be greatly appreciated! Thanks!</p>