0

I'm writing a udp protocol code, one of the functions in the server side is to write the received string into a output .txt file.

Here is part of my code about this problem

for (;;) 
{

  fpout = fopen("out.txt","w");
  if(fpout == NULL){
    printf("Error!");
    exit(1);
  }

  int receive = 1;     
  while(receive)
  {
    bytes_recd = recvfrom(sock_server, &pkt, STRING_SIZE, 0, (struct sockaddr *) &client_addr, &client_addr_len);
    if(pkt.length == 0)
      receive = 0;
    printf("Server received Sentence is: %s\n     with length of %d\n", pkt.databytes, pkt.length);

    printf("Server received %d packet\n",pkt.seqnum);
    strcpy(sentence, pkt.databytes);
    fprintf(fpout, "%s", sentence);
  }
  fclose(fpout);
}

pkt is the struct I transmitted, pkt.databytes is the string part I want to write into the .txt file. But right now, nothing writes into the out.txt file.

EricBkc
  • 369
  • 2
  • 4
  • 12
  • Maybe nothing gets written because nothing get received, did you consider writing some debug statement (for example printing the packet length after it get received), or using a debugger ? – dvhh Dec 01 '15 at 00:47
  • @dvhh, thanks for answer. Yes, I do write the print function, and it shows me all the receive part are working well. – EricBkc Dec 01 '15 at 00:53
  • could you update the code with the print statement ? – dvhh Dec 01 '15 at 00:55
  • Updated, the two printf will show me the details about the received packet. – EricBkc Dec 01 '15 at 01:01
  • could you fflush(fpout) ? – dvhh Dec 01 '15 at 01:09
  • I'm not very familiar with fflush function, would you like to tell me more about this, thanks. – EricBkc Dec 01 '15 at 01:12
  • My guess is that your file output is cached before getting written to disk, fflush will request the cached content to be written to disk (see : http://linux.die.net/man/3/fflush ). – dvhh Dec 01 '15 at 01:14
  • 1
    You need to check the value of `bytes_recd` before accessing anything in `pkt`. If `bytes_recd` is too small (or zero or negative), then the fields in `pkt` are not valid. Also, `pkt.databytes` needs to be a properly NUL terminated string (it's hard to tell if it is without seeing the client code). – user3386109 Dec 01 '15 at 01:16
  • The `fclose(fpout)` will automatically `fflush(fpout)`. – user3386109 Dec 01 '15 at 01:17
  • For now all the printf in my code shows exactly correct and bytes_recd is larger than 0, does it mean the fields in pkt is valid? And the pkt.databytes is one line of a .txt file, I think the last character is \n, does it mean the NUL terminated string you said? – EricBkc Dec 01 '15 at 01:25
  • 1
    The `fopen` truncates the file each time it is called. Either take the `fopen` and `fclose` out of the loop and use `fflush` within the loop or use `"a"` instead of `"w"` in the `fopen`. – kaylum Dec 01 '15 at 01:25
  • I don't know the minimum value for `bytes_recd` since you haven't shown the definition of the `pkt` structure. The `'\n'` is the newline, a `'\0'` is a NUL terminator. Either the client needs to send the `'\0'` as part of the packet, or the server code needs to put the `'\0'` at the end of the string. Usually, the server code adds the `'\0'` since that's safer. – user3386109 Dec 01 '15 at 01:29
  • Thank you guys, it works. I think the point is I miss the NUL terminated, and I also change "a " instead of "w" in fopen. Thanks again. – EricBkc Dec 01 '15 at 01:41

1 Answers1

0

Thank you guys, it works. I think the point is I miss the NUL terminated, and I also change "a " instead of "w" in fopen. Thanks again. – EricBkc

Armali
  • 18,255
  • 14
  • 57
  • 171