title: Client-server text file transfer using UDP & sockets in C++, server creates a blank file
body: <p>First of all, I'd like to point out that i'm not a good programmer, so please be patient with me. The logic in the program is as follows: the client sends the server a text file, the server saves it under a different name. In short, very similar to this topic:
  " <a href="https://stackoverflow.com/questions/11254447/file-transfer-server-client-in-c-on-linux-using-socket">File transfer server/client using socket</a> " except that I'm using a different protocol. I think I've managed to succesfully send the file, but it seems like the server, afters creating a blank file, stucks in a loop/doesn't write anything in it.</p>
  
  <p>Here's the code for the client:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt; /* memset() */
  #include &lt;sys/time.h&gt; /* select() */ 
  #include &lt;errno.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;  
  
  
  #define REMOTE_SERVER_PORT 1500
  #define MAX_MSG 100
  #define PLIK    "/home/aatami/Documents/programowanie/serwer/plik.txt"
  #define LENGTH 512
  int main(int argc, char *argv[]) {
  
    int sd, rc, i,fd;
    unsigned int cliLen;
    struct sockaddr_in cliAddr, remoteServAddr;
    struct hostent *h;
    char  buf[LENGTH];
    /* check command line args */
    if(argc&lt;3) {
      printf("usage : %s &lt;server&gt; &lt;data1&gt; ... &lt;dataN&gt; \n", argv[0]);
      exit(1);
    }
  
    /* get server IP address (no check if input is IP address or DNS name */
    h = gethostbyname(argv[1]);
    if(h==NULL) {
      printf("%s: unknown host '%s' \n", argv[0], argv[1]);
      exit(1);
    }
  
    printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h-&gt;h_name,
       inet_ntoa(*(struct in_addr *)h-&gt;h_addr_list[0]));
  
    remoteServAddr.sin_family = h-&gt;h_addrtype;
    memcpy((char *) &amp;remoteServAddr.sin_addr.s_addr, 
       h-&gt;h_addr_list[0], h-&gt;h_length);
    remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
  
    /* socket creation */
    sd = socket(AF_INET,SOCK_DGRAM,0);  /* port 0 - system gets the first free one */ 
    if(sd&lt;0) {
      printf("%s: cannot open socket \n",argv[0]);
      exit(1);
    }
  
    /* bind any port */
    cliAddr.sin_family = AF_INET;
    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    cliAddr.sin_port = htons(0);
  
    rc = bind(sd, (struct sockaddr *) &amp;cliAddr, sizeof(cliAddr));
    if(rc&lt;0) {
      printf("%s: cannot bind port\n", argv[0]);
      exit(1);
    }
  
  
    /* send data */
    for(i=2;i&lt;argc;i++) {
      rc = sendto(sd, argv[i], strlen(argv[i])+1, 0, 
          (struct sockaddr *) &amp;remoteServAddr, 
          sizeof(remoteServAddr));
  
      if(rc&lt;0) {
        printf("%s: cannot send data %d \n",argv[0],i-1);
        close(sd);
        exit(1);
      }
      /* send text file */
  
  
          char sdbuf[LENGTH]; 
          printf("[Client] Sending %s to the Server... ", PLIK);
          FILE *fs = fopen(PLIK, "r");
          if(fs == NULL)
          {
              printf("ERROR: File %s not found.\n", PLIK);
              exit(1);
          }
  
          bzero(sdbuf, LENGTH); 
          int fs_block_sz; 
          while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) &gt; 0)
          {
              if(send(sd, sdbuf, fs_block_sz, 0) &lt; 0)
              {
                  fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", PLIK,           errno);
                  break;
              }
              bzero(sdbuf, LENGTH);
          }
          printf("Ok File %s from Client was Sent!\n", PLIK);
  
  
    }
  
    return 1;
  
  }
  </code></pre>
  
  <p>And the code for the server:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt; /* close() */
  #include &lt;string.h&gt; /* memset() */
  #include &lt;errno.h&gt;
  #define LENGTH 512
  #define LOCAL_SERVER_PORT 1500
  #define MAX_MSG 100
  #define PLIKSERV    "/home/aatami/Documents/programowanie/serwer/plikserv.txt"
  int main(int argc, char *argv[]) { /* licznik argumentow, tablica argumentow */
  
    int sd, rc,nsockfd;
    unsigned int n,cliLen;
    struct sockaddr_in cliAddr, servAddr;
    char msg[MAX_MSG];
     char  buf[512],sbuf[LENGTH];
    /* socket creation */
    sd=socket(AF_INET, SOCK_DGRAM, 0);
    if(sd&lt;0) {
      printf("%s: cannot open socket \n",argv[0]);
      exit(1);
    }
  
    /* bind local server port */
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = htons(LOCAL_SERVER_PORT);
    rc = bind (sd, (struct sockaddr *) &amp;servAddr,sizeof(servAddr));
    if(rc&lt;0) {
      printf("%s: cannot bind port number %d \n", 
         argv[0], LOCAL_SERVER_PORT);
      exit(1);
    }
  
    printf("%s: waiting for data on port UDP %u\n", 
         argv[0],LOCAL_SERVER_PORT);
  
  
  
    /* server infinite loop */
    while(1) {
  
      /* init buffer */
      memset(msg,0x0,MAX_MSG);
  
  
      /* receive message */
      cliLen = sizeof(cliAddr);
      n = recvfrom(sd, msg, MAX_MSG, 0, 
           (struct sockaddr *) &amp;cliAddr, &amp;cliLen);
  
      if(n&lt;0) {
        printf("%s: cannot receive data \n",argv[0]);
        continue;
      }
  
      /* print received message */
      printf("%s: from %s:UDP%u : %s \n", 
         argv[0],inet_ntoa(cliAddr.sin_addr),
         ntohs(cliAddr.sin_port),msg);
  
           unsigned int sin_size = sizeof(struct sockaddr_in);
  
  
  
  
          FILE *fr = fopen(PLIKSERV, "a");
          if(fr == NULL)
              printf("File %s Cannot be opened file on server.\n", PLIKSERV);
          else
          {
              bzero(sbuf, LENGTH); 
              int fr_block_sz = 0;
              while((fr_block_sz = recvfrom(sd, buf, LENGTH, 0,(struct sockaddr *) &amp;cliAddr, &amp;cliLen)) &gt; 0) 
              {
                  int write_sz = fwrite(buf, sizeof(char), fr_block_sz, fr);
                  if(write_sz &lt; fr_block_sz)
                  {
                      perror("File write failed on server.\n");
                  }
                  bzero(buf, 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); 
          }
  
    }/* end of server infinite loop */
  
  return 0;
  
  }
  </code></pre>
  
  <p>Any help greatly appreciated!</p>