title: Sending files from client to server using sockets in C
body: <p>The program is supposed to send the contents of a file from the client side to an output file on the server side. However, my code is working for few files and not working for most of the files. For example if I try to copy content of a file called <code>morefood.txt</code> to an output file say <code>picolo.txt</code>, nothing is copied.</p>
  
  <p>Server code:</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  
  int main(int argc, char *argv[]){
  
    int fd =0, confd = 0;
    struct sockaddr_in serv_addr;
  
    char buff[1025];
    int num;
  
    fd = socket(AF_INET, SOCK_STREAM, 0);
    printf("Socket created\n");
  
    memset(&amp;serv_addr, '0', sizeof(serv_addr));
    memset(buff, '0', sizeof(buff));
  
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5000);
  
    bind(fd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr));
    listen(fd, 10);
  
    FILE* fp = fopen( "picolo.txt", "wb");
  
    if(fp == NULL){
        fprintf(stderr, "something went south");
        return 1;
    }
  
    while(1){
  
        confd = accept(fd, (struct sockaddr*)NULL, NULL);
  
        char recvbuff[10];
  
        int b = recv(confd, recvbuff, 10, 0);
  
        while(b&gt;0)
       {
            fwrite(recvbuff, 1, b, fp);
  
            b = recv(confd, recvbuff, 10, 0);
  
       }
     close(confd);
    }
  
  return 0;
  
  }
  </code></pre>
  
  <p>Client code:</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  
  int main(int argc, char *argv[]){
  
      int sfd =0, n=0;
      char rbuff[1024];
  
      struct sockaddr_in serv_addr;
  
      memset(rbuff, '0', sizeof(rbuff));
      sfd = socket(AF_INET, SOCK_STREAM, 0);
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(5000);
      serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  
      connect(sfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr));
  
      FILE *fp = fopen("morefood.txt", "rb");
  
      if(fp == NULL){
          fprintf(stderr, "oh no!");
          return 1;
      }
  
      char sendbuffer[100];
  
      int b = fread(sendbuffer, 1, sizeof(sendbuffer), fp);
  
      while(!feof(fp)){
          send(sfd, sendbuffer, b, 0);
          b = fread(sendbuffer, sizeof(sendbuffer), 1, fp);
      }
  
      return 0;
  
     }
  </code></pre>