1. void recv_data(int sd, char **buffer) {
  2.  
  3. int int_buf_size = 10;
  4. char int_buf[int_buf_size];
  5. char *up_to_now = NULL;
  6. int num_of_bytes_written = 0;
  7.  
  8. int num_of_bytes_received = 0;
  9. int done = 0;
  10.  
  11. do {
  12. num_of_bytes_received = recv(sd, int_buf, int_buf_size, 0);
  13. assert(num_of_bytes_received!=-1);
  14.  
  15. if (num_of_bytes_received == 0) {
  16. done = 1;
  17. }
  18.  
  19. else {
  20. printf("bytes received = %d \n",num_of_bytes_received);
  21. up_to_now = realloc(up_to_now, num_of_bytes_received);
  22. memcpy(&up_to_now[num_of_bytes_written], int_buf, num_of_bytes_received);
  23. printf("bytes written = %d \n",num_of_bytes_written);
  24. num_of_bytes_written = num_of_bytes_written + num_of_bytes_received;
  25. printf("bytes written = %d \n",num_of_bytes_written);
  26. printf("%s\n",up_to_now);
  27.  
  28. }
  29. } while (!done);
  30.  
  31.  
  32. printf("bytes written final = %d \n",num_of_bytes_written);
  33.  
  34. *buffer = malloc(num_of_bytes_written);
  35. memcpy(*buffer,up_to_now,num_of_bytes_written);
  36. printf("%s\n",*buffer);
  37.  
  38. }