title: transfer files through sockets in c
body: <p>I want to implement a program that transfer any files from the server to the client. I must use read/write functions to read and write data(its for school assignment). Here is the code for server and client.</p>
  
  <p><strong>server.c</strong></p>
  
  <pre><code>    char buffer[512];        
  
      if( (file = open(strTable[1], O_RDONLY)) == -1 ) { perror("Open"); }
  
      while( read(file, buffer, sizeof(buffer)) != 0 )
      {
          size = strlen(buffer)+1;
          if( write(newsock, &amp;size, sizeof(size)) &lt; 0 ) { perror("Write"); exit(1); }
  
          write_all(newsock, buffer, strlen(buffer)+1);
      }
  
      size = 4;
      if( write(newsock, &amp;size, sizeof(size)) &lt; 0 ) { perror("Write"); exit(1); }
      write_all(newsock, "end", 4);
  </code></pre>
  
  <p>It opens strTable[1] (which contains the file that i want to read) it reads sizeof(buffer) bytes and then i send to client how much bytes i will write to socket and after that i send the bytes. Here is write_all function.</p>
  
  <pre><code>int write_all(int sock, char* buffer, int size)
  {
     int nwrite, sent = 0;
  
     while( sent &lt; size )
     {
         if( (nwrite = write(sock, buffer + sent, size - sent)) &lt; 0 ) 
         { perror("Write"); exit(1); }
  
         sent += nwrite;
     }
  
     return sent;
  }
  </code></pre>
  
  <p>client.c</p>
  
  <pre><code>if( (file = open(absolute, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1 )
                  { perror("Open"); }    
  
  while( true )
          {
              received = 0;
  
              /* Read the desired readable size */
              if( read(sock, &amp;size, sizeof(size)) &lt; 0 ) 
              { perror("Read"); pthread_exit(NULL); }
  
              /* Read all data */
              while( received &lt; size )
              {
                  if( (nread = read(sock, buffer + received, size - received)) &lt; 0 )
                  { perror("Read"); pthread_exit(NULL); }
  
                  received += nread;
              }
  
              if( strncmp(buffer, "end", 4) == 0 ) { break; }
  
              write_all(file, buffer, strlen(buffer)+1);
          }
  </code></pre>
  
  <p>The client opens a file (absolute) and writes in it what it reads. It first reads how much size it has to read and then it doesnt stop to read until it reaches that size. If the client reads "end" then server has stop the writing to the socket so the client stops reading.</p>
  
  <p>The problem is that i cant open the files after the transfer. I read some images that i cant open. I read also and one file.txt which i write some random words, this seems to be copied right but it has some garbage too(its the photo number 2).  Why i getting this and how can i transfer files from sockets correctly? </p>
  
  <p><a href="https://i.stack.imgur.com/XQ0Vg.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p><a href="https://i.stack.imgur.com/sdJQB.png" rel="nofollow noreferrer">enter image description here</a></p>