title: Array of chars in socket client and socket server written in ANSI C
body: <p>I'm trying to write simple client and server based on sockets in C.
  The client sends the size of the char array (including last cell for '\0') and then array of chars.
  Server gets the size and tries to allocate memory for the array of chars from the client.
  After this, the server looks for a space and copy the characters from index 0 to space, pastes it at the end of the array and sends it back to the client (if there is no space, then the server sends doubled array).</p>
  
  <p>I've noticed that sometimes I'm getting 
   --
  |00|
  |02|
   --
  char at the end of the received array from the server.
  I've run the server and the client using valgrind and it showed that problem is in the server.</p>
  
  <p>Code below.</p>
  
  <p>Client:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  
  
  int
  main ()
  {
      int sockfd;
      socklen_t len;
      struct sockaddr_in address;
      int result;
  
      char ch;
      char *string;
      int i;
  
      sockfd = socket (AF_INET, SOCK_STREAM, 0);
  
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = inet_addr ("127.0.0.1"); 
      address.sin_port = htons (9734);
      len = sizeof (address);
  
      result = connect (sockfd, (struct sockaddr *) &amp;address, len);
  
      if (result == -1)
      {
          perror ("oops: netclient");
          exit (1);
      }
  
      string = (char*)malloc(sizeof(char));
  
      for (i =0; ch = getchar(); i++)
      {
          string = (char*)realloc(string, (i+1)*sizeof(char));
  
          if(ch != '\n')
              string[i] = ch;
          else if (ch == '\n')
          {
              string[i]='\0';
              break;
          }
      }
  
      printf("%s\n", string);
      printf("%d\n", i);
      i=i+1;
  
      write (sockfd, &amp;i, 4);
  
      write (sockfd, string, i);
  
  
      read (sockfd, &amp;i, 4);
  
  
      string = (char*)realloc(string, i*sizeof(char));
  
      read (sockfd, string, i); 
  
      printf("String recieved: %s\n", string);
  
      close (sockfd);
  
      free(string);
  
      exit (0);
  }
  </code></pre>
  
  <p>And server:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  int
  main ()
  {
      int server_sockfd, client_sockfd; 
      socklen_t server_len, client_len; 
      struct sockaddr_in server_address; 
      struct sockaddr_in client_address; 
  
      server_sockfd = socket (AF_INET, SOCK_STREAM, 0); 
      server_address.sin_family = AF_INET; /*ipv4*/
      server_address.sin_addr.s_addr = htonl (INADDR_ANY); 
      server_address.sin_port = htons (9734);
      server_len = sizeof (server_address);
      bind (server_sockfd, (struct sockaddr *) &amp;server_address, server_len); 
  
  
      listen (server_sockfd, 5); 
      signal (SIGCHLD, SIG_IGN);
  
      while (1)
      {
          char *string;
          int i, j, k, l=0; /*variables to iteration*/
          printf ("server waiting\n");
  
  
          client_len = sizeof (client_address);
          client_sockfd = accept (server_sockfd,
                  (struct sockaddr *) &amp;client_address,
                  &amp;client_len); 
  
  
          if (fork () == 0)
          {
  
  
              read (client_sockfd, &amp;i, 4);
              printf("recieved int %d\n", i);
              string = (char*)malloc(sizeof(char) * (i));
              read (client_sockfd, string, i);
              printf("\nSTRING recieved: %s \n", string);
  
              for(j=0; string[j]!='\0'; j++)
              {
                  if(string[j] == ' ')
                  {
                      i = i+j; /*size to enlarge*/
                      break;
                  }
              }
  
              /*sending new size*/
              write(client_sockfd, &amp;i, 4);
  
              /*enlarge the string*/
              string = (char*)realloc(string, i*sizeof(char));
  
              for (k = i-j-1; k &lt; i-1 ; k ++)
              {
                  string[k] = string[l];
                  l++;
              }
              string[k+1] = '\0';
  
              write(client_sockfd, string, i);
  
  
              close (client_sockfd);
  
              free(string);
  
              exit (0);
          }
  
  
  
          else
          {
              close (client_sockfd);
          }
      }
  }
  </code></pre>