title: TCP program to reverse a string in C language giving half result
body: <p>i have written a TCP client/server program to reverse a string however the result is coming partially right.</p>
  
  <p><strong>Client:</strong></p>
  
  <pre><code>#‎include‬&lt;stdio.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;sys/types.h&gt;
  void main()
  { 
     struct sockaddr_in server;
     int sock;
     char buffer[20];
  
     printf("Enter a string :");
     scanf("%s",buffer);
  
     server.sin_family = AF_INET;
     server.sin_port = 2000;
     server.sin_addr.s_addr = inet_addr("127.0.0.1");
     sock = socket(AF_INET,SOCK_STREAM, 0);
  
     connect(sock, (struct sockaddr*) &amp;server, sizeof(server));
     send(sock, buffer, sizeof(buffer), 0);
     recv(sock, buffer, sizeof(buffer), 0);
  
     printf("The reverse string is: %s",buffer);
     printf("\n");
  }
  </code></pre>
  
  <p><strong>SERVER:</strong> </p>
  
  <pre><code>‪#‎include‬&lt;stdio.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;string.h&gt;
  void main()
  { 
     struct sockaddr_in server, client;
     char buffer[20], temp;
     int n, sock, newsock, i, j;
  
     sock = socket(AF_INET, SOCK_STREAM, 0);
     server.sin_family = AF_INET;
     server.sin_port = 2000;
     server.sin_addr.s_addr = inet_addr("127.0.0.1");
     bind(sock, (struct sockaddr*) &amp;server, sizeof(server));
  
     listen(sock, 2);
     n = sizeof(client);
     new sock = accept(sock, (struct sockaddr*) &amp;client, &amp;n);
     for(;;)
     {
        recv(newsock, buffer, sizeof(buffer), 0);
        i = 0; 
        j = strlen(buffer) - 1;
        while(i&lt;j)
        { 
           temp = buffer[i]; 
           buffer[i] = buffer[j]; 
           buffer[j] = temp; 
           i++; 
           j--;
         }
     send(newsock, buffer, sizeof(buffer), 0);
     }
  }
  </code></pre>
  
  <p><strong>Now when i run it and suppose i input the string as "hello how are you" but the result is coming as only "olleh" and skipping rest of the sentence. am i missing something here?</strong></p>