send the file periodically through socket in windows

hello .. anyone know how to send a file periodically from client to server through socket ?

thanks
Load the file into memory and stream it over a socket.
Last edited on
i did.
but how to send it periodically ?

client only send a file to server but not periodically
You'll have to use a timing library. Depending on how frequently you want to send the file, you could probably just use the built in time library: http://www.cplusplus.com/reference/ctime/

You would basically have a loop that looks something like this:
1
2
3
4
5
6
7
8
9
int sentTime = time(0);
while (true)
{
    if (time(0)-sentTime>=60) //one minute has elapsed
    {
        sentTime = time(0);
        sendFile(); //this obviously isn't built in, you need to replace it with your own code
    }
}


Note that this will eat up CPU time like no one's business. Depending on what networking library you're using it may provide some sort of waiting function. You would have to look at the documentation though. If you go that route the loop would look a little different:

1
2
3
4
5
while (true)
{
    sendFile();
    sleep(60000); //assuming this function takes milliseconds, this will "pause" your program for one minute
}
which one can i use ? the first one or the second one ?
and is it also implemented in server to receive the data ?

i ever used the second one . but look like it only send the original file.

the things is , I want to send a file periodically from client to server in C++. say, every 10 seconds. "send.txt" is the file that i want to send to server through socket

"send.txt" contains "123456" for the first 10 seconds. i change the file content MANUALLY by opening "send.txt" and adding 78910 11 12 so it becomes "123456 78910 11 12" i change it still in the interval 1 -10 seconds while the program is still executed.

the server should receive "123456 78910 11 12" for the next 10 seconds. but the problem is the file that i received is still the original one (123456) and look like it never changes to
123456 78910 11 12.
Last edited on
You just need to reload the data before you send it. I'm guessing you loaded the file contents and then kept sending them, without ever reloading them to pick up any changes.
here is my client code :

// client.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>


SOCKET clientsock;
WSADATA winsock;
sockaddr_in serverAddr , addr;
int Addrlen = sizeof(serverAddr);
FILE *File;
unsigned long Size;
char *Buffer;

void startClient() {


WSAStartup(MAKEWORD(2,2), &winsock);

if(LOBYTE(winsock.wVersion) != 2 || HIBYTE(winsock.wVersion) != 2 ){

WSACleanup();

}


clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(6091);

connect(clientsock,(sockaddr*)&addr,sizeof(addr));

printf("socket connected... \n");


}


void sending() {


//preparing the file

File = fopen("B:\\send.txt","rb");

if(!File) {

printf("",WSAGetLastError());
}

printf("File open ok ! \n");


fseek(File,0,SEEK_END);
Size = ftell(File);
fseek(File,0,SEEK_SET);


char cisi[10];
sprintf(cisi, "%i",Size);


send(clientsock,cisi,10,0); // file size sent

File = fopen("B:\\send.txt","rb");
fseek(File,0,SEEK_END);
Size = ftell(File);
fseek(File,0,SEEK_SET);
printf("Success...\n");

Buffer = (char*) malloc (Size+1) ;
fread(Buffer,Size,1,File);
fclose(File);

send(clientsock,Buffer,Size,0); //File binary sent
free(Buffer);
printf("sending finished ... \n");

}









int _tmain(int argc, _TCHAR* argv[])
{


startClient();



while(1){

sending();
Sleep(5000);
}




system("PAUSE");
return 0;


}





and here is my server code :

// server.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>

SOCKET servsocket, ClientAcc;
WSAData winsock;
sockaddr_in addr,incomingAddress;
int addrlen = sizeof(sockaddr_in);
int addresslen = sizeof(incomingAddress);
char *Filesize = new char[1024];
int Size = 0;
char *Buffer = new char[Size];
FILE *File;

void start_s() {

//socket initialization
WSAStartup(MAKEWORD(2,2), &winsock);

//socket check

if(LOBYTE(winsock.wVersion) !=2 || HIBYTE(winsock.wVersion) != 2 ) {

WSACleanup();
}


servsocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_port = htons(6091);
bind(servsocket, (sockaddr*)&addr, sizeof(addr));

listen(servsocket, 5);

ClientAcc = accept(servsocket, (sockaddr*)&incomingAddress, &addresslen);

char *ClientIP = inet_ntoa(incomingAddress.sin_addr);
int ClientPort = ntohs(incomingAddress.sin_port);
printf("Client Connected ... \n");
printf("IP : %s:%d\n", ClientIP, ClientPort);


}



void receive() {


//receive the file size

recv(ClientAcc,Filesize,1024,0);
Size = atoi((const char*)Filesize);
printf("File size : %d\n",Size);


//receive the file

Buffer = (char*)malloc(Size+1);
int file_dit, total_file = 0 ;
while(total_file < Size) {
ZeroMemory(Buffer,Size);
if((file_dit = recv(ClientAcc,Buffer,Size,0)) < 0 ){

goto END;

} else {


total_file += file_dit;

File = fopen("B:\\fileDiterima.txt", "wb");
fwrite((const char*)Buffer,1,file_dit,File);
fclose(File);
Sleep(1000);

}

END:
printf("File received ... \n");
free(Buffer);
closesocket(ClientAcc);
WSACleanup();
getchar();


}
}



int _tmain(int argc, _TCHAR* argv[])
{
start_s();

while(1){

receive();
Sleep(5000);
}


system("PAUSE");
return 0;


}



can you tell me where the mistake is ? and can you give also the solution ?

Topic archived. No new replies allowed.