title: Socket communication between C and python code
body: <p>I have a python socket server that sends strings to a C program (client) that prints the string, then sends it back to the python program that prints it too. The communication works, but if it sends a long string, then a short one, the current and previous data kinda merges. Here's the example:</p>
  
  <p>Output:</p>
  
  <pre><code>5.0standing0.0                //That is how it should look like
  5.0walk0ing0.055111512313e-17 //After the long string
  </code></pre>
  
  <p>python code (just gets called in a loop):</p>
  
  <pre><code>import socket
  import sys
  
  # Create a TCP/IP socket
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  # Bind the socket to the port
  server_address = ('localhost', 8888)
  print &gt;&gt;sys.stderr, '\nstarting up on %s port %s\n' % server_address
  sock.bind(server_address)
  
  # Listen for incoming connection
  sock.listen(1)
  
  def send_control(state,xdiff,xactual):
    connection, client_address = sock.accept()
    connection.sendall(str(xactual)+str(state)+str(xdiff))
    data = connection.recv(2000)
    print data
    data = '\0'
  </code></pre>
  
  <p>C code (gets called in a loop too):</p>
  
  <pre><code>void comm_func() 
  {
    int sock;
    int i=0;
    struct sockaddr_in server;
    char message[1024] , server_reply[2000];
  
    //Create socket
    sock = socket(AF_INET , SOCK_STREAM , 0);
    if (sock == -1)
    {
        printf("Could not create socket");
    }
    // puts("Socket created");
  
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );
  
    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
    {
        perror("connect failed. Error");
    }
  
    // Receive a reply from the server
    if( recv(sock , server_reply , 2000 , 0) &lt; 0)
    {
        puts("recv failed");
    }
    puts(server_reply);
    write(sock,server_reply,50);
    for (i=0; i&lt; strlen(server_reply); i++)
    {
      server_reply[i] = '\0';
    }
    close(sock);
  }
  </code></pre>
  
  <p>Can anybody help me?</p>