I have a C server that writes packet data to an output file. The problem is I terminate the server in terminal using control c, and I've noticed that my output file is never finished writing to. Is there a way to make sure that my output file is completely written to before quitting?
(I know I could just let the server run longer so I'm sure the data I want has been written, but I'm looking for an alternative method)
Thanks
Here's my code for reference.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/time.h>
int PORT_NUM = 0; 
int RecordRate = 3000; 
typedef struct timeval timeval;
timeval time_;
void error(const char *msg)
{
    perror(msg);
    exit(1);
}
int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[1000000];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     FILE *fp;
     PORT_NUM = atoi(argv[1]);  
     fp = fopen(argv[2],"w");   
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
            error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     //portno = atoi(argv[1]);
     portno = PORT_NUM;
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
        error("ERROR on binding");
     listen(sockfd,10);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
     if (newsockfd < 0) 
            error("ERROR on accept");
    int counter = 0;    
    int total_bytes_sent = 0;
     while(1){  
         bzero(buffer,1000000);
         n = read(newsockfd,buffer,999999);
         if (n < 0) {
             error("ERROR reading from socket");
         }
         else if (n != 0) {
             total_bytes_sent += n;
             gettimeofday(&time_, NULL);
             if(counter%RecordRate==0){
             printf("counter  %d \n", counter); 
             fprintf(fp,"\"%d\",\"%ld\",\"%d\",\"%d\",\"%d\"\n", counter, time_.tv_sec, time_.tv_usec, n,total_bytes_sent);
             }
             counter++; 
             //print format: packet number, time Sec, time milli Sec, bytes in packet, total bytes sent 
            // example: "11182","1465921447","196422","3100","26821100"
         }
     }
     fclose(fp);
     close(newsockfd);
     close(sockfd);
     return 0; 
     }
 
     
     
     
    