title: send file from client to server using socket in c on linux
body: <p>I'm very new in C language and linux and English is not my mother language. Sorry for those in advance.
  What I need to is to send <code>.avi</code> or <code>.mp4</code> files from a client to a server using socket on Linux. I can send the file from client to server but the video on the  server is not working.</p>
  
  <p>When I try to play the video, I keep getting an error like "could not determine the type of stream". When I checked the original video on client, the size was 5,787,969 bytes but the size of the video transferred on server is 5,786,954 bytes. I think this is because of data loss when I transfer the file.</p>
  
  <p>How do I fix this?</p>
  
  <p>This is my code below:</p>
  
  <h3>Server</h3>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;     // read, write
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/types.h&gt;  // socket, bind, accept, open
  #include &lt;sys/socket.h&gt; // socket, bind, listen, accept
  #include &lt;sys/stat.h&gt;   // open
  #include &lt;fcntl.h&gt;      // open
  #include &lt;errno.h&gt;
  
  #define PORT    5500
  #define MAXBUF  1024
  
  int main() {
      int server_sockfd;
      int client_sockfd;
      int des_fd; // file num
      struct sockaddr_in serveraddr, clientaddr;
      int client_len, read_len, file_read_len;    // length
      char buf[MAXBUF];
  
      int check_bind;
      client_len = sizeof(clientaddr);
  
      /* socket() */
      server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if(server_sockfd == -1) {
          perror("socket error : ");
          exit(0);
      }
  
      /* bind() */
      bzero(&amp;serveraddr, sizeof(serveraddr));
      serveraddr.sin_family       = AF_INET;
      serveraddr.sin_addr.s_addr  = htonl(INADDR_ANY);
      serveraddr.sin_port         = htons(PORT);
  
      if(bind(server_sockfd, (struct sockaddr *)&amp;serveraddr, sizeof(serveraddr)) &gt; 0) {
          perror("bind error : ");
          exit(0);
      }
  
      /* listen */
      if(listen(server_sockfd, 5) != 0) {
          perror("listen error : ");
      }
  
      while(1) {
          char file_name[MAXBUF]; // local val
          memset(buf, 0x00, MAXBUF);
  
          /* accept() */
          client_sockfd = accept(server_sockfd, (struct sockaddr *)&amp;clientaddr, &amp;client_len);
          printf("New Client Connect : %s\n", inet_ntoa(clientaddr.sin_addr));
  
          /* file name */
          read_len = read(client_sockfd, buf, MAXBUF);
          if(read_len &gt; 0) {
              strcpy(file_name, buf);
              printf("%s &gt; %s\n", inet_ntoa(clientaddr.sin_addr), file_name);
          } else {
              close(client_sockfd);
              break;
          }
  
          /* create file */
  
          des_fd = open(file_name, O_WRONLY | O_CREAT | O_EXCL, 0700);
          if(!des_fd) {
              perror("file open error : ");
              break;
          }   
          /* file save */
          while(1) {
              memset(buf, 0x00, MAXBUF);
              file_read_len = read(client_sockfd, buf, MAXBUF);
              write(des_fd, buf, file_read_len);
              if(file_read_len == EOF | file_read_len == 0) {
                  printf("finish file\n");
                  break;
              }
  
  
          }
  
  
          close(client_sockfd);
          close(des_fd);
      }
      close(server_sockfd);
      return 0;
  }
  </code></pre>
  
  <h3>Client</h3>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  
  #define PORT    5500
  #define IP      "127.0.0.1"
  #define MAXBUF  1024
  
  int main() {
      struct sockaddr_in serv_addr;
      int     s;
      int         sourse_fd;
      char        buf[MAXBUF];
      int         file_name_len, read_len;
      /* socket() */
      s = socket(AF_INET, SOCK_STREAM, 0);
      if(s == -1) {
          return 1;
      }
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = inet_addr(IP);
      serv_addr.sin_port = htons(PORT);
  
      if(connect(s, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) == -1) {
          perror("connect : ");
          printf("fail to connect.\n");
          close(s);
          return 1;
      }
  
      memset(buf, 0x00, MAXBUF);
      printf("write file name to send to the server:  ");
      scanf("%s", buf);
  
      printf(" &gt; %s\n", buf);
      file_name_len = strlen(buf);
  
      send(s, buf, file_name_len, 0);
      sourse_fd = open(buf, O_RDONLY);
      if(!sourse_fd) {
          perror("Error : ");
          return 1;
      }
  
      while(1) {
          memset(buf, 0x00, MAXBUF);
          read_len = read(sourse_fd, buf, MAXBUF);
          send(s, buf, read_len, 0);
          if(read_len == 0) {
              break;
          }
  
      }
  
      return 0;
  }
  </code></pre>