title: File transfer server/client using socket
body: <p>I am trying to make a file transfer between server and client, but is working very badly. Basically what needs to happen is:<br>
  1) The client send a txt file to the server (I called it "quotidiani.txt")<br>
  2) The server saves it in another txt file ("receive.txt")<br>
  3) The server runs a script on it that modifies it and saves it with another name ("output.txt")<br>
  4) The server send the file back to the client that saves it (on the same socket) with the name (final.txt)</p>
  
  <p>The problem is that the first file (quotidiani.txt) is read just for a little part, and then there are some errors. I'd like someone to help me understand and correct my errors.</p>
  
  <p>Here's my code:  </p>
  
  <p>client.c:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;ctype.h&gt;          
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  
  #define PORT 20000
  #define LENGTH 512 
  
  
  void error(const char *msg)
  {
      perror(msg);
      exit(1);
  }
  
  int main(int argc, char *argv[])
  {
      /* Variable Definition */
      int sockfd; 
      int nsockfd;
      char revbuf[LENGTH]; 
      struct sockaddr_in remote_addr;
  
      /* Get the Socket file descriptor */
      if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      {
          fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor! (errno = %d)\n",errno);
          exit(1);
      }
  
      /* Fill the socket address struct */
      remote_addr.sin_family = AF_INET; 
      remote_addr.sin_port = htons(PORT); 
      inet_pton(AF_INET, "127.0.0.1", &amp;remote_addr.sin_addr); 
      bzero(&amp;(remote_addr.sin_zero), 8);
  
      /* Try to connect the remote */
      if (connect(sockfd, (struct sockaddr *)&amp;remote_addr, sizeof(struct sockaddr)) == -1)
      {
          fprintf(stderr, "ERROR: Failed to connect to the host! (errno = %d)\n",errno);
          exit(1);
      }
      else 
          printf("[Client] Connected to server at port %d...ok!\n", PORT);
  
      /* Send File to Server */
      //if(!fork())
      //{
          char* fs_name = "/home/aryan/Desktop/quotidiani.txt";
          char sdbuf[LENGTH]; 
          printf("[Client] Sending %s to the Server... ", fs_name);
          FILE *fs = fopen(fs_name, "r");
          if(fs == NULL)
          {
              printf("ERROR: File %s not found.\n", fs_name);
              exit(1);
          }
  
          bzero(sdbuf, LENGTH); 
          int fs_block_sz; 
          while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) &gt; 0)
          {
              if(send(sockfd, sdbuf, fs_block_sz, 0) &lt; 0)
              {
                  fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
                  break;
              }
              bzero(sdbuf, LENGTH);
          }
          printf("Ok File %s from Client was Sent!\n", fs_name);
      //}
  
      /* Receive File from Server */
      printf("[Client] Receiveing file from Server and saving it as final.txt...");
      char* fr_name = "/home/aryan/Desktop/progetto/final.txt";
      FILE *fr = fopen(fr_name, "a");
      if(fr == NULL)
          printf("File %s Cannot be opened.\n", fr_name);
      else
      {
          bzero(revbuf, LENGTH); 
          int fr_block_sz = 0;
          while((fr_block_sz = recv(sockfd, revbuf, LENGTH, 0)) &gt; 0)
          {
              int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
              if(write_sz &lt; fr_block_sz)
              {
                  error("File write failed.\n");
              }
              bzero(revbuf, LENGTH);
              if (fr_block_sz == 0 || fr_block_sz != 512) 
              {
                  break;
              }
          }
          if(fr_block_sz &lt; 0)
          {
              if (errno == EAGAIN)
              {
                  printf("recv() timed out.\n");
              }
              else
              {
                  fprintf(stderr, "recv() failed due to errno = %d\n", errno);
              }
          }
          printf("Ok received from server!\n");
          fclose(fr);
      }
      close (sockfd);
      printf("[Client] Connection lost.\n");
      return (0);
  }
  </code></pre>
  
  <p>server.c  </p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;ctype.h&gt;          
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  
  #define PORT 20000 
  #define BACKLOG 5
  #define LENGTH 512 
  
  void error(const char *msg)
  {
      perror(msg);
      exit(1);
  }
  
  int main ()
  {
      /* Defining Variables */
      int sockfd; 
      int nsockfd; 
      int num;
      int sin_size; 
      struct sockaddr_in addr_local; /* client addr */
      struct sockaddr_in addr_remote; /* server addr */
      char revbuf[LENGTH]; // Receiver buffer
  
      /* Get the Socket file descriptor */
      if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
      {
          fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor. (errno = %d)\n", errno);
          exit(1);
      }
      else 
          printf("[Server] Obtaining socket descriptor successfully.\n");
  
      /* Fill the client socket address struct */
      addr_local.sin_family = AF_INET; // Protocol Family
      addr_local.sin_port = htons(PORT); // Port number
      addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address
      bzero(&amp;(addr_local.sin_zero), 8); // Flush the rest of struct
  
      /* Bind a special Port */
      if( bind(sockfd, (struct sockaddr*)&amp;addr_local, sizeof(struct sockaddr)) == -1 )
      {
          fprintf(stderr, "ERROR: Failed to bind Port. (errno = %d)\n", errno);
          exit(1);
      }
      else 
          printf("[Server] Binded tcp port %d in addr 127.0.0.1 sucessfully.\n",PORT);
  
      /* Listen remote connect/calling */
      if(listen(sockfd,BACKLOG) == -1)
      {
          fprintf(stderr, "ERROR: Failed to listen Port. (errno = %d)\n", errno);
          exit(1);
      }
      else
          printf ("[Server] Listening the port %d successfully.\n", PORT);
  
      int success = 0;
      while(success == 0)
      {
          sin_size = sizeof(struct sockaddr_in);
  
          /* Wait a connection, and obtain a new socket file despriptor for single connection */
          if ((nsockfd = accept(sockfd, (struct sockaddr *)&amp;addr_remote, &amp;sin_size)) == -1) 
          {
              fprintf(stderr, "ERROR: Obtaining new Socket Despcritor. (errno = %d)\n", errno);
              exit(1);
          }
          else 
              printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr));
  
          /*Receive File from Client */
          char* fr_name = "/home/aryan/Desktop/receive.txt";
          FILE *fr = fopen(fr_name, "a");
          if(fr == NULL)
              printf("File %s Cannot be opened file on server.\n", fr_name);
          else
          {
              bzero(revbuf, LENGTH); 
              int fr_block_sz = 0;
              while((fr_block_sz = recv(nsockfd, revbuf, LENGTH, 0)) &gt; 0) 
              {
                  int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
                  if(write_sz &lt; fr_block_sz)
                  {
                      error("File write failed on server.\n");
                  }
                  bzero(revbuf, LENGTH);
                  if (fr_block_sz == 0 || fr_block_sz != 512) 
                  {
                      break;
                  }
              }
              if(fr_block_sz &lt; 0)
              {
                  if (errno == EAGAIN)
                  {
                      printf("recv() timed out.\n");
                  }
                  else
                  {
                      fprintf(stderr, "recv() failed due to errno = %d\n", errno);
                      exit(1);
                  }
              }
              printf("Ok received from client!\n");
              fclose(fr); 
          }
  
          /* Call the Script */
          system("cd ; chmod +x script.sh ; ./script.sh");
  
          /* Send File to Client */
          //if(!fork())
          //{
              char* fs_name = "/home/aryan/Desktop/output.txt";
              char sdbuf[LENGTH]; // Send buffer
              printf("[Server] Sending %s to the Client...", fs_name);
              FILE *fs = fopen(fs_name, "r");
              if(fs == NULL)
              {
                  fprintf(stderr, "ERROR: File %s not found on server. (errno = %d)\n", fs_name, errno);
                  exit(1);
              }
  
              bzero(sdbuf, LENGTH); 
              int fs_block_sz; 
              while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs))&gt;0)
              {
                  if(send(nsockfd, sdbuf, fs_block_sz, 0) &lt; 0)
                  {
                      fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
                      exit(1);
                  }
                  bzero(sdbuf, LENGTH);
              }
              printf("Ok sent to client!\n");
              success = 1;
              close(nsockfd);
              printf("[Server] Connection with Client closed. Server will wait now...\n");
              while(waitpid(-1, NULL, WNOHANG) &gt; 0);
          //}
      }
  }
  </code></pre>