send modified file periodically from server to client c++ socket programming

  c++, file-transfer, sockets, tcp

In my code basic connectivity and file transfer(.csv) work. But I want to send a file periodically from server to client in C++ every 10 seconds with updated data. I tried many things from google but didn’t find anything. I tried while loop to send the file(.csv) 300 times to a client but got, the same file again and again, not an updated one. I have no idea, how to do this. please help.

My code is here-

Server code –

        FILE *fp;
token=strtok(NULL," n");
cout<<"Filename given is: "<<token<<endl;
data_port=data_port+1;
if(data_port==atoi(argv[1])){
    data_port=data_port+1;
}
sprintf(port,"%d",data_port);
datasock=create_socket(data_port);              //creating socket for data connection
send(connfd, port,MAXLINE,0);                   //sending port no. to client
datasock=accept_conn(datasock); 
int j=0;
int count = 1;
while(j<365)
{   
    //accepting connnection by client
    if ((fp=fopen(token,"r"))!=NULL)

    {
    //size of file
        send(connfd,"1",MAXLINE,0);
        fseek (fp , 0 , SEEK_END);
        lSize = ftell (fp);
        rewind (fp);
        num_blks = lSize/MAXLINE;
        num_last_blk = lSize%MAXLINE; 
        sprintf(char_num_blks,"%d",num_blks);
        send(connfd, char_num_blks, MAXLINE, 0);
        //cout<<num_blks<<" "<<num_last_blk<<endl;

        for(i= 0; i < num_blks; i++) { 
            fread (buffer,sizeof(char),MAXLINE,fp);
            send(datasock, buffer, MAXLINE, 0);
        //cout<<buffer<<"   "<<i<<endl;
        }
        sprintf(char_num_last_blk,"%d",num_last_blk);
        send(connfd, char_num_last_blk, MAXLINE, 0);
        if (num_last_blk > 0) { 
            fread (buffer,sizeof(char),num_last_blk,fp);
            send(datasock, buffer, MAXLINE, 0);
            //cout<<buffer<<endl;
        }
        fclose(fp);
        cout<<"File upload done.n";
        j++;
        sleep(10);
    }   

    else{
        send(connfd,"0",MAXLINE,0);
        }
    }

Client code –

                FILE *fp;
recv(sockfd, port, MAXLINE,0);
data_port=atoi(port);
datasock=create_socket(data_port,argv[1]);
token=strtok(NULL," n");
recv(sockfd,message,MAXLINE,0);
if(strcmp("1",message)==0){
    if((fp=fopen(token,"w"))==NULL)
        cout<<"Error in creating filen";
    else
    {
        recv(sockfd, char_num_blks, MAXLINE,0);
        num_blks=atoi(char_num_blks);
        for(i= 0; i < num_blks; i++) { 
            recv(datasock, buffer, MAXLINE,0);
            fwrite(buffer,sizeof(char),MAXLINE,fp);
            //cout<<buffer<<endl;
        }
        recv(sockfd, char_num_last_blk, MAXLINE,0);
        num_last_blk=atoi(char_num_last_blk);
        if (num_last_blk > 0) { 
            recv(datasock, buffer, MAXLINE,0);
            fwrite(buffer,sizeof(char),num_last_blk,fp);
            //cout<<buffer<<endl;
        }
        fclose(fp);

        std::string filename = "python.py";
        std::string command = "python3 ";
        command += filename;
        system(command.c_str());
    }
}

This to basic file transfer code using get command.

thank you in advance.

Source: Windows Questions C++

LEAVE A COMMENT