title: Knowing when Client has finished reading from the socket in C
body: <p>I am writing a client-server program in c where I have to send multiple image files from the server to the client. Is there any way for the server to know when the client has finished reading from socket, creating the image file locally, and successfully written to it? P.S. I already tried sending a message to the socket and when I try to read the socket from the server, the program hangs. Any help will be much appreciated. 
  Here is a function from the Server code which sends the file to socket:</p>
  
  <pre><code>while(1)
  {
    unsigned char buf[256] = {0};
  
    int n = fread(buf,1,256,fp);
  
    if(n&gt;0) { send(sockfd,buf,n,0); }
    if(n&lt;256) {
      if(feof(fp)) 
         printf("Sent to socket\n");
         break;
      }
    }
    fclose(fp);
  }
  
  char buf[5]
  read(sockfd,buf,5);
  if(strcmp(buf,"ready")==0) //send more files  
  </code></pre>
  
  <p>And here is a function from the client to write to the file:</p>
  
  <pre><code> FILE* fp;
   fp = fopen(file_path,"ab");
   char buf[256];
   int num;
   int total=0;
   while(( num = recv(sockfd,buf,256,0))&gt;0)
   {
    total+=num;
    fwrite(buf,1,num,fp);
   }
  
   fclose(fp);
  
   write(sockfd,"ready",5);
  
   }
  </code></pre>
  
  <p>When I do a read on the server after one file transfer, the program hangs.</p>