title: Sending file (client to server and server to client)
body: <p>I have client.c and server.c , I'm trying to send a file from client to server and from server to client...
  The file is received by the server but when the server sends the file , the client stops or does not receive the file ,
  The sendFile() function:</p>
  
  <pre><code>void sendfile(int fd,char* filename)
  {
      char buff[0x1000]; 
      FILE *file = fopen(filename, "rb"); 
      if (!file)
      {
          printf("Can't open file for reading"); 
          return;
      }
      while (!feof(file)) 
      { 
          int rval = fread(buff, 1, sizeof(buff), file); 
          if (rval &lt; 1)
          {
              printf("Can't read from file\n");
              fclose(file);
              return;
          }
  
          int off = 0;
          do
          {
              int sent = send(fd, &amp;buff[off], rval - off, 0);
              printf("Sending ...\n");
              if (sent &lt; 1)
              {
                  printf("Can't write to socket");
                  fclose(file);
                  return;
              }
              printf("[SendFile]Fisierul a fost trimis!\n");
              off += sent;
          }
          while (off &lt; rval);
      } 
   fclose(file);
  } 
  </code></pre>
  
  <p>The getFile():</p>
  
  <pre><code>void getFile(int fd,char* filename)
   {
      int rval; 
      char buff[0x1000]; 
      FILE *file = fopen(filename, "w+"); 
      if (!file)
      {
          printf("Can't open file for writing");
          return;
      }
  
      do
      {
          rval = recv(fd, buff, sizeof(buff), 0);
          if (rval &lt; 0)
          {
              printf("Can't read from socket");
              fclose(file);
              return;
          }
  
          if (rval == 0)
              break;
  
          int off = 0;
          do
          {
              int written = fwrite(&amp;buff[off], 1, rval - off, file);
          printf("[server]Writing %d bytes\n",written);
              if (written &lt; 1)
              {
                  printf("Can't write to file");
                  fclose(file);
                  return;
              }
  
              off += written;
          }
          while (off &lt; rval);
      printf("[server]File received!\n"); 
      }  
      while(1);
      fclose(file); 
   }
  </code></pre>
  
  <p>The client main():</p>
  
  <pre><code>int main (int argc, char *argv[])
  {
    int sd;
    struct sockaddr_in server;
  
    if (argc != 2)
      {
        printf ("[client] Sintaxa: %s &lt;filename&gt;\n", argv[0]);
        return -1;
      }
  
    if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
      {
        perror ("[client] Eroare la socket().\n");
        return errno;
      }
  
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_port = htons (port);
  
    if (connect (sd, (struct sockaddr *) &amp;server,sizeof (struct sockaddr)) == -1)
      {
        perror ("[client]Eroare la connect().\n");
        return errno;
      }
      sendfile(sd,"test.cpp");
      printf("File sent\n");
      getFile(sd,"server.c");
      close (sd);
  }
  </code></pre>
  
  <p>The loop from server.c</p>
  
  <pre><code>while (1)
      {
        int client;
        int length = sizeof (from);
  
        printf ("[server]Asteptam la portul %d...\n",port);
        fflush (stdout);
  
        client = accept (sd, (struct sockaddr *) &amp;from, &amp;length);
  
        if (client &lt; 0)
        {
        perror ("[server]Eroare la accept().\n");
        continue;
        }
  
      if((pid=fork()) == 0 )
      {
  
      getFile(client,"test.cpp");
          sendfile(client,"server.c");
          printf("File sent\n");
      close(client);
    } 
  
   }
  </code></pre>