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.