title: Sending large files over socket in c
body: <p>So this code works ok for small files of up to 20KB but when i try to send or receive larger files it stops reading at some point. Two functions for client and server, to send and receive files. I know it might have some leaks and stuff but I will fix that later. At first I want it to work properly. BUF_SIZE=512</p>
  
  <pre><code>// SENDING BYTES
  void send_bytes(int connfd, int client_number, char **command_parts)
  {
      FILE *fp;
      fp = fopen(command_parts[1], "r");
      int i = 0;
      // FINDING BYTE SIZE OF THE FILE
      fseek(fp, 0, SEEK_END); /* PUTTING FILE POINTER TO THE END OF THE FILE */
      int num_of_bytes = ftell(fp);
      fseek(fp, 0, SEEK_SET); /* RESETTING FILE POINTER TO THE BEGINNING OF THE FILE */
      char *sendbuff = malloc(num_of_bytes * sizeof(char));
      memset(sendbuff, '\0', num_of_bytes);
      // SENDING TOTAL BYTES TO SEND
      while (write(connfd, itoa(num_of_bytes), 10) &lt; 0)
      {
      }
      printf("\nClient #%d --&gt; Size of file sent: %d\n", client_number, num_of_bytes);
  
      // IF BYTES MORE THAN 512 SENDING IT IN PARTS
      if(num_of_bytes &gt; BUF_SIZE)
      {
          int bytes_left_to_send = num_of_bytes;
          char *tempbuff = calloc(BUF_SIZE, sizeof(char));
          while(bytes_left_to_send &gt; 0)
          {
              if(bytes_left_to_send &gt;= BUF_SIZE)
              {
                  // READING 512 BYTES AND PASSING IT TO TEMPBUFF
                  for (i = 0; i &lt; BUF_SIZE; i++)
                  {
                      fread(&amp;tempbuff[i], 1, 1, fp);
                  }
                  // SENDING BYTES
                  while (write(connfd, tempbuff, BUF_SIZE) &lt; 0)
                  {
                  }
                  bytes_left_to_send = bytes_left_to_send - BUF_SIZE;
                  memset(tempbuff, '\0', BUF_SIZE);
              }
              else
              {
                  // READING 512 BYTES AND PASSING IT TO TEMPBUFF
                  for (i = 0; i &lt; bytes_left_to_send; i++)
                  {
                      fread(&amp;tempbuff[i], 1, 1, fp);
                  }
                  // SENDING BYTES
                  while (write(connfd, tempbuff, bytes_left_to_send) &lt; 0)
                  {
                  }
                  bytes_left_to_send = bytes_left_to_send - bytes_left_to_send;
                  memset(tempbuff, '\0', BUF_SIZE);
              }
          }
      }
      else
      {
          // READING EACH BYTE AND PASSING IT TO SENDBUFF
          for (i = 0; i &lt; num_of_bytes; i++)
          {
              fread(&amp;sendbuff[i], 1, 1, fp);
          }
          // SENDING BYTES
          while (write(connfd, sendbuff, num_of_bytes) &lt; 0)
          {
          }
      }
  
      // FREEING MEMORY
      fclose(fp);
      free(sendbuff);
      printf("\nClient #%d --&gt; File sent\n", client_number);
  }
  // RECEIVING BYTES
  void recv_bytes(int connfd, int client_number, char **command_parts)
  {
      int bytes_read = 0, bytes_read_in_total = 0, i = 0;
      char *num_of_bytes = malloc(10 * sizeof(char));
      char *recvbuff = NULL;
      FILE *fp;
      fp = fopen(command_parts[2], "w");
      // RECEIVING BYTES TO RECEIVE
      while (read(connfd, num_of_bytes, 10) &lt; 0)
      {
      }
      printf("\nClient #%d --&gt; Size of file received: %d\n", client_number, atoi(num_of_bytes));
      recvbuff = malloc(atoi(num_of_bytes) * sizeof(char)); /* ALLOCATING recvbuff WITH RECEIVED SIZE */
      memset(recvbuff, '\0', atoi(num_of_bytes));
  
      // IF BYTES MORE THAN 512 RECEIVING IT IN PARTS
      if(atoi(num_of_bytes) &gt; BUF_SIZE)
      {
          int bytes_left_to_read = atoi(num_of_bytes);
          char *tempbuff = calloc(BUF_SIZE, sizeof(char));
          while(bytes_left_to_read &gt; 0)
          {
              if(bytes_left_to_read &gt;= BUF_SIZE)
              {
                  // READING BYTES
                  while (bytes_read = read(connfd, tempbuff, BUF_SIZE) &lt; 0)
                  {
                  }
                  bytes_read = strlen(tempbuff);
                  // WRITING BYTES READ SO FAR TO THE FILE
                  for (i = 0; i &lt; bytes_read; i++)
                  {
                      fwrite(&amp;tempbuff[i], 1, 1, fp);
                  }
                  bytes_left_to_read = bytes_left_to_read - bytes_read;
                  memset(tempbuff, '\0', BUF_SIZE);
              }
              else
              {
                  // READING BYTES
                  while (bytes_read = read(connfd, tempbuff, bytes_left_to_read) &lt; 0)
                  {
                  }
                  bytes_read = strlen(tempbuff);
                  // WRITING BYTES READ SO FAR TO THE FILE
                  for (i = 0; i &lt; bytes_read; i++)
                  {
                      fwrite(&amp;tempbuff[i], 1, 1, fp);
                  }
                  bytes_left_to_read = bytes_left_to_read - bytes_read;
                  memset(tempbuff, '\0', BUF_SIZE);
              }
          }
      }
      else
      {
          // RECEIVING BYTES
          while (bytes_read_in_total &lt; atoi(num_of_bytes))
          {
              while (bytes_read = read(connfd, recvbuff, atoi(num_of_bytes)) &lt; 0)
              {
              }
              bytes_read = strlen(recvbuff);
              bytes_read_in_total = bytes_read_in_total + bytes_read;
  
              // WRITING BYTES READ SO FAR TO THE FILE
              for (i = 0; i &lt; bytes_read; i++)
              {
                  fwrite(&amp;recvbuff[i], 1, 1, fp);
              }
              memset(recvbuff, '\0', atoi(num_of_bytes));
          }
      }
  
      free(recvbuff);
      fclose(fp);
      printf("\nClient #%d --&gt; File received\n", client_number);
  }
  </code></pre>