title: Is there any way to know the amount of bytes send from the client to the server and process the recv() in networks
body: <p>I am trying to build a chat application between the server and the client. My doubt is for sending information from the client or from the server I was able to handle the partial send with the help of the loop, but I am unable to find out the length of the send data bytes from the client to the server or from the server to the client, thereby having problem in creating the memory for the received bytes and printing. </p>
  
  <p>My chat function code for the client:</p>
  
  <pre><code>
  int chat_function(int sockfd) 
  { 
      char ch;
      char *buf;
      char *newp;
      int ret_send = 0;
      int ret_recv = 0;
      int buf_size = 0; 
  
      while(1) { 
          printf("From client, enter the message : ");
          buf = (char *)malloc(sizeof(char));
          if (buf == NULL)
              return -1;  
          while ((ch = getchar()) != '\n') {
              buf[buf_size++] = ch;
              newp = (char *)realloc(buf, (buf_size + 1) * sizeof(char));
              if ( newp == NULL) {
                  free(buf);
                  return -1;
              }
              buf = newp; 
          }
          buf[buf_size] = '\0';
          ret_send = send_all(sockfd, buf, buf_size);
          if (ret_send == -1)
              error(1, errno, "error in send() function call\n");
          memset(buf, 0, buf_size);
          ret_recv = recv_all(sockfd, buf, buf_size);
          if (ret_recv == -1) {
              error(1, errno, "error in recv() function call\n");
          } else if (ret_recv == -2) {
              printf("Oops the server has closed the connection\n");
              free(buf);
              break;
          }
          printf("From Server : %s", buf); 
          if ((strncmp(buf, "exit", 4)) == 0) { 
              printf("Client Exit...\n");
              free(buf); 
              break; 
          }
          free(buf);
      } 
  } 
  </code></pre>
  
  <p>For handling partial send:</p>
  
  <pre><code>int send_all(int sockfd, char *buf, int buf_size)
  {
      int bytes_left = 0;
      size_t send_bytes = 0;
  
      bytes_left = buf_size
      while (1) {
          send_bytes = send(fd, buf, bytes_left, 0);
          if (send_bytes == -1)
              return -1;
          buf = buf + send_bytes;
          bytes_left = bytes_left - send_bytes;
          if (bytes_left == 0)
              break;
      }
      return 0;
  }
  </code></pre>