title: How to use socket with a Python client and a C++ server
body: <p>I have a simple client/server program.</p>
  
  <p>The client is written in python as this :</p>
  
  <pre><code>import socket
  import sys
  
  HOST, PORT = "localhost", 50007
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  s.connect((HOST, PORT))
  for x in range(0, 10000):
      print("Step 1")
      s.send(b'Hello')
      print("Step 2")
      print(str(s.recv(1000)))
      print(x)
  </code></pre>
  
  <p>And I wrote a server in python like this :</p>
  
  <pre><code>import socket
  
  HOST = ''
  PORT = 50007
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((HOST, PORT))
  s.listen(1)
  conn, addr = s.accept()
  
  while True:
      data = conn.recv(1024)
      conn.sendall(data)
  </code></pre>
  
  <p>I want to create a C++ version of the server. I did this :</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  using namespace std;
  
  #define SERVER_PORT htons(50007)
  
  int main() {
  
          char buffer[1000];
          int n;
  
          int serverSock=socket(AF_INET, SOCK_STREAM, 0);
  
          sockaddr_in serverAddr;
          serverAddr.sin_family = AF_INET;
          serverAddr.sin_port = SERVER_PORT;
          serverAddr.sin_addr.s_addr = INADDR_ANY;
  
          /* bind (this socket, local address, address length)
             bind server socket (serverSock) to server address (serverAddr).  
             Necessary so that server can use a specific port */ 
          bind(serverSock, (struct sockaddr*)&amp;serverAddr, sizeof(struct sockaddr));
  
          // wait for a client
          /* listen (this socket, request queue length) */
          listen(serverSock,1);
  
          while (1 == 1) {
                  bzero(buffer, 1000);
  
                  sockaddr_in clientAddr;
                  socklen_t sin_size=sizeof(struct sockaddr_in);
                  int clientSock=accept(serverSock,(struct sockaddr*)&amp;clientAddr, &amp;sin_size);
  
                  //receive a message from a client
                  n = read(clientSock, buffer, 500);
                  cout &lt;&lt; "Confirmation code  " &lt;&lt; n &lt;&lt; endl;
                  cout &lt;&lt; "Server received:  " &lt;&lt; buffer &lt;&lt; endl;
  
                  strcpy(buffer, "test");
                  n = write(clientSock, buffer, strlen(buffer));
                  cout &lt;&lt; "Confirmation code  " &lt;&lt; n &lt;&lt; endl;
          }
          return 0;
  }
  </code></pre>
  
  <p>It works one time but it block at the line <code>str(s.recv(1000))</code> of the client.</p>
  
  <p>This is the output from the client :</p>
  
  <pre><code>Step 1
  Step 2
  b'test'
  0
  Step 1
  Step 2
  </code></pre>
  
  <p>And this is the output from the server :</p>
  
  <pre><code>code  5
  Server received:  Hello
  code  4
  code  5
  Server received:  Hello
  code  4
  </code></pre>
  
  <p>As you can see, the communication is like this :</p>
  
  <ol>
  <li>The client send a message</li>
  <li>The server receive the message</li>
  <li>The server send a message</li>
  <li>The client send another message</li>
  <li>The server receive a message and seems to send another message but the client did not receive it.</li>
  </ol>
  
  <p>What is my error?</p>