I'm a new comer in this stackoverflow.
I successfully sent a file.txt and received it . And I want to send the file through a socket periodically and receive it periodically, too. This is a server client program. Client should send the data and server should receive it with an interval time.
Anyone know how to do this? I used this way:
Problem is the file is not sent.
Here is my client code :
void Inwinsock(){
WSAStartup(MAKEWORD(2,2), &winsock);
if (LOBYTE(winsock.wVersion) != 2 || HIBYTE(winsock.wVersion) != 2 ) {
WSACleanup();
}
}
void ClientSock() {
clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(6091);
}
int _tmain(int argc, _TCHAR* argv[])
{
Inwinsock();
ClientSock();
start:
if (connect(clientSock, (sockaddr*)&addr, sizeof(addr)) < 0 ) {
Sleep(5000);
goto start;
}
printf("Socket Connected ......... \n");
FILE *File;
char *Buffer;
unsigned long Size;
File = fopen("B:\Filesend.txt","rb");
if(!File){
printf("", WSAGetLastError());
goto END;
}
printf("File open ok ! \n");
fseek(File,0,SEEK_END);
Size = ftell(File);
fseek(File,0,SEEK_SET);
printf("file size succeed...\n");
Buffer = (char*) malloc (Size+1);
fread(Buffer,Size,1,File);
char cisi[10];
sprintf(cisi, "%i", Size);
fclose(File);
printf("sending data....\n");
send(clientSock,cisi,10,0); //file size sent
send(clientSock,Buffer,Size,0); // File Binary sent
free(Buffer);
printf("sending finished....\n");
END:
closesocket(clientSock);
WSACleanup();
getchar();
return 0;
system("PAUSE");
}
and here are my server code :
void iniSocket() {
WSAStartup(MAKEWORD(2,2), &winsock);
if (LOBYTE(winsock.wVersion) != 2 || HIBYTE(winsock.wVersion) != 2) {
WSACleanup();
}
}
void opSock(){
servSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ZeroMemory(&addr , sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(6091);
bind(servSocket, (sockaddr*)&addr, sizeof(addr));
}
void sockList(){
if (listen(servSocket, 5) == SOCKET_ERROR ) {
printf("listen error %d\n", WSAGetLastError());
}else
{
printf("listen succeed....\n");
}
}
void receive() {
if(recv(ClientAcc,Filesize,10,0)){
Size = atoi((const char*)Filesize);
printf("File size : %d\n",Size);
}
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("fileReceived.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[])
{
while(1)
{
iniSocket();
opSock();
sockList();
if (ClientAcc = accept(servSocket, (sockaddr*)&incommingAddress, &addresslen))
{
char *ClientIP = inet_ntoa(incommingAddress.sin_addr);
int ClientPort = ntohs (incommingAddress.sin_port);
printf("Client connected ....\n" );
printf("IP : %s:%d\n ", ClientIP, ClientPort);
receive();
}
}
return 0;
system("PAUSE");
}