title: C Sending files with Berkeley socket
body: <p>I'm trying to send files on a Client-Server application written in c.</p>
  
  <p>Client should upload files and server should receive files.
  Client side : </p>
  
  <pre><code>if(fstat(fd,&amp;file_stat) &lt; 0){
      perror("Fstat error");
      return -1;
  }
  
  
  FILE *filepointer = fopen(filename,"rb");
  
  if(filepointer == NULL){
      perror("Opening file error");
      return -1;
  }
  
  strcat(sendbuf,"8"); // option number
  strcat(sendbuf,client_account.user); //user who is sending a file
  strcat(sendbuf,"p1.txt"); // file name
  printf("sendbuf : %s\n",sendbuf );
  if(write(sock_fd,sendbuf,strlen(sendbuf)) &lt; 0 ){
      perror("Writing error");
      return -1;
  }
  
  /* Check server's answer */
  if((nread = read(sock_fd,buffer,sizeof(buffer))) &lt; 0){
      perror("Reading error");
      return -1;
  }
  buffer[nread] = '\0';
  printf("Buffer : %s : %d",buffer,atoi(buffer));
  if(atoi(buffer) != GENERIC_OK){
      printf("Error occurred\n");
      return -1;
  }
  
  sprintf(file_size,"%lld",file_stat.st_size);
  
  /* Writing file size */
  if((nwritten = write(sock_fd,file_size,sizeof(file_size))) &lt; 0){
      perror("Writing error");
      return -1;
  }
  
  memset(buffer,0,sizeof(buffer));
  /* Check second server's answer */
  if((nread = read(sock_fd,buffer,sizeof(buffer))) &lt; 0){
      perror("Reading error");
      return -1;
  }
  buffer[nread] = '\0';
  printf("Buffer : %s : %d",buffer,atoi(buffer));
  if(atoi(buffer) != GENERIC_OK){
      printf("Error occurred\n");
      return -1;
  }
  
  
  while(1){
  
      nbytes = fread(sendbuf,1,sizeof(sendbuf),filepointer);
      /* There are bytes to send */
      printf("Sendbuf : %s \n" , sendbuf);
      if(nbytes &gt; 0){
          write(sock_fd,sendbuf,nbytes);
      }
  
      if(nbytes &lt; 256){
          if(feof(filepointer) || ferror(filepointer))
              break;
      }
  }
  </code></pre>
  
  <p>Server side : </p>
  
  <pre><code>... first buffer is received well ...`
  /* WRITE TO CLIENT TO CONTINUE */
  if(write(sock_fd,"500",strlen("500")) &lt; 0){ /*GENERIC OK*/
      perror("Writing error");
      return -1;
  }
  
  memset(recvBuf,0,sizeof(recvBuf));
  /*RECEIVING FILE SIZE */
  if((nread = read(sock_fd,buffer,sizeof(buffer)))&lt; 0){
      perror("Reading error");
      return -1;
  }
  buffer[nread] = '\0';
  
  file_size = atoi(buffer);
  printf("file size : %d\n",file_size);
  
  
  
  if((fd = open(filename, O_CREAT | O_RDWR | O_TRUNC,  S_IRWXU)) &lt; 0){
      perror("File opening error"); /* file already exists */
  
  }
  
  recv_file = fopen(filename,"wb");
  if(recv_file == NULL){
      perror("Opening error");
      return -1;
  }
  
  remaining_data = file_size;/*i think processes are blocking on while loops*/
  while(((nread = read(sock_fd,recvBuf,sizeof(recvBuf))) &gt; 0) &amp;&amp; remaining_data &gt; 0){ 
      recvBuf[nread] = '\0';
      printf("%d bytes received : %s \n",nread,recvBuf);
      fwrite(recvBuf,1,nread,recv_file);
      remaining_data -= nread;
  }
  
  if(nread &lt; 0){
      perror("Reading error");
  }
  </code></pre>
  
  <p>I've tried to use <code>sendfile</code> function but I'm on Mac OS and it's not supported. Have you got any advice for me?</p>
  
  <p>It should go like this : </p>
  
  <p>1) client sends a buffer to server announcing it's going go send a file with its name - OK</p>
  
  <p>2) server receives this buffer and sends a generic ok code to client - OK</p>
  
  <p>3) client sends the size of the file to the server - OK</p>
  
  <p>4) server receives this buffer and sends a generic ok code to client - OK</p>
  
  <p>5) client read stuff from file and sends it to the server - NOT OK</p>
  
  <p>6) server receives stuff from the client and writes stuff into the file - NOT OK</p>