Kgc
asked on
server client file transfer problem
Dear Experts,
Greetings, This is my program:
This program is a server-client file transfer program.
The client will send the file localtion (full path of the document at server)
to the server.
Server will zip the file (external program,PKzip) and send the zipped file size to client to prepare the buffer.
After Zipped, the server will open the zipped file in binary and send to client.
The problem i facing is:
the client can receive the 1st request file (less than 10KB)
but when the 2nd file (> 20MB)
the client receive failed.
when I change the 2nd file size to less than 10KB,
it's work.
below is the coding:
=============
//************************ ********** ********** *//
// client's getFile functiion
//************************ ********** ********** *//
bool GetFile(SOCKET theClient, char file[256], int &theSize)
{
int iRet;
FILE *fout;
char fileName[256];
CClientDlg Dlg;
char counter[128];
int TotalBytes = 0;
strcpy(fileName, file);
iRet = send(theClient, fileName, sizeof(fileName), 0);
if (iRet == SOCKET_ERROR)
{
int err = WSAGetLastError();
Dlg.MessageBox((char *)err,0,0);//TEXT("Error at send begin %d",err), 0, 0);
return false;
}
//get the file size
iRet = recv(theClient, counter, sizeof(counter), 0);
if (iRet == SOCKET_ERROR)
{
return false;
}
/*
// i try to wait until to get the file size
while (iRet != atoi(counter))
{
iRet = recv(theClient, counter, sizeof(counter), 0);
if (iRet == SOCKET_ERROR)
{
return false;
}
};*/
int FileSize = atoi(counter);
if (FileSize == 0)
return false;
theSize = FileSize;
char RootDir[MAX_PATH];
strcpy(RootDir, fileName);
DirStrip(RootDir, fileName);
char *buffer = new char[FileSize];
int b_receive;
iRet = 0;
for (int i = 0; i < FileSize; i += iRet)
{
b_receive = FileSize - TotalBytes;
iRet = recv(theClient, &buffer[i], b_receive, 0);
TotalBytes += iRet;
if (iRet == SOCKET_ERROR)
{
int a = WSAGetLastError();
closesocket(theClient);
Dlg.MessageBox("error error", 0, 0);
return false;
}
}
//this gets the total bytes sent from server to check if file was recv correct
char total[12];
itoa(TotalBytes, total, 10);
recv(theClient, total, sizeof(total), 0);
if (atoi(total) != TotalBytes)
{
return false;
}
char *tempIndex = new char [3];
strcpy(fileName, "c:\\temp\\WVS");
strcat(fileName, itoa(downloadIndex,tempInd ex,10));
strcat(fileName, ".zip");
strcpy(fileDownload[downlo adIndex].z ipFileName , fileName);
downloadIndex++;
delete[] tempIndex;
if( (fout = fopen(fileName, "wb")) != NULL)
{
fwrite(buffer, FileSize,1,fout);
fclose(fout);
}
delete[] buffer;
closesocket(theClient);
return true;
}
//************************ ********** ********** *//
// server's sendFile functiion
//************************ ********** ********** *//
bool SendFile(SOCKET sock)
{
char fileName[256];
char TempZip[256];
FILE *fin;
int iRet;
int counter = 0;
int TotalBytes = 0;
char *tempChar = new char [12];
memset(fileName, 0, sizeof(fileName));
iRet = recv(sock, fileName, sizeof(fileName), 0);
if (iRet == SOCKET_ERROR)
MessageBox(NULL, "Error receiving filename", 0, 0);
strcpy(TempZip, "C:\\Temp\\");
strcat(TempZip, itoa(fileCounter, tempChar, 10));
strcat(TempZip, ".zip ");
strcpy(tempChar, TempZip);
strcat(TempZip,fileName);
fileCounter++;
HINSTANCE hInst = ShellExecute(0,
"open", // Operation to perform
"c:\\PKzip_Utility\\pkzip. exe", // Application name
(LPCTSTR)TempZip,
NULL, // Default directory
SW_HIDE); // Hide the zip screen
strcpy(fileName, tempChar);
while((fin = fopen(fileName,"rb")) == NULL); // if the zip file not exist yet then become NULL
counter = filesize(fin);
char FileSize[128];
itoa(counter, FileSize, 10);
iRet = send(sock, FileSize, sizeof(FileSize), 0);
if (iRet == SOCKET_ERROR)
{
return false;
}
char *ToSend = new char[counter];
fseek(fin, 0, SEEK_SET); //move to begining of file again
fread(ToSend, counter,1,fin);
iRet = send(sock, ToSend, counter, 0);
TotalBytes += iRet;
if (iRet == SOCKET_ERROR)
{
fclose(fin);
closesocket(sock);
int error=WSAGetLastError();
if (error==WSAEMSGSIZE)
return false;
}
//send the actual bytes sent
char total[12];
itoa(TotalBytes, total, 10);
send(sock, total, sizeof(total), 0);
//file transfer is complete, so close the socket and file stream
delete [] ToSend;
fclose(fin);
closesocket(sock);
return true;
}
Thanks.
regards,
Kgc
Greetings, This is my program:
This program is a server-client file transfer program.
The client will send the file localtion (full path of the document at server)
to the server.
Server will zip the file (external program,PKzip) and send the zipped file size to client to prepare the buffer.
After Zipped, the server will open the zipped file in binary and send to client.
The problem i facing is:
the client can receive the 1st request file (less than 10KB)
but when the 2nd file (> 20MB)
the client receive failed.
when I change the 2nd file size to less than 10KB,
it's work.
below is the coding:
=============
//************************
// client's getFile functiion
//************************
bool GetFile(SOCKET theClient, char file[256], int &theSize)
{
int iRet;
FILE *fout;
char fileName[256];
CClientDlg Dlg;
char counter[128];
int TotalBytes = 0;
strcpy(fileName, file);
iRet = send(theClient, fileName, sizeof(fileName), 0);
if (iRet == SOCKET_ERROR)
{
int err = WSAGetLastError();
Dlg.MessageBox((char *)err,0,0);//TEXT("Error at send begin %d",err), 0, 0);
return false;
}
//get the file size
iRet = recv(theClient, counter, sizeof(counter), 0);
if (iRet == SOCKET_ERROR)
{
return false;
}
/*
// i try to wait until to get the file size
while (iRet != atoi(counter))
{
iRet = recv(theClient, counter, sizeof(counter), 0);
if (iRet == SOCKET_ERROR)
{
return false;
}
};*/
int FileSize = atoi(counter);
if (FileSize == 0)
return false;
theSize = FileSize;
char RootDir[MAX_PATH];
strcpy(RootDir, fileName);
DirStrip(RootDir, fileName);
char *buffer = new char[FileSize];
int b_receive;
iRet = 0;
for (int i = 0; i < FileSize; i += iRet)
{
b_receive = FileSize - TotalBytes;
iRet = recv(theClient, &buffer[i], b_receive, 0);
TotalBytes += iRet;
if (iRet == SOCKET_ERROR)
{
int a = WSAGetLastError();
closesocket(theClient);
Dlg.MessageBox("error error", 0, 0);
return false;
}
}
//this gets the total bytes sent from server to check if file was recv correct
char total[12];
itoa(TotalBytes, total, 10);
recv(theClient, total, sizeof(total), 0);
if (atoi(total) != TotalBytes)
{
return false;
}
char *tempIndex = new char [3];
strcpy(fileName, "c:\\temp\\WVS");
strcat(fileName, itoa(downloadIndex,tempInd
strcat(fileName, ".zip");
strcpy(fileDownload[downlo
downloadIndex++;
delete[] tempIndex;
if( (fout = fopen(fileName, "wb")) != NULL)
{
fwrite(buffer, FileSize,1,fout);
fclose(fout);
}
delete[] buffer;
closesocket(theClient);
return true;
}
//************************
// server's sendFile functiion
//************************
bool SendFile(SOCKET sock)
{
char fileName[256];
char TempZip[256];
FILE *fin;
int iRet;
int counter = 0;
int TotalBytes = 0;
char *tempChar = new char [12];
memset(fileName, 0, sizeof(fileName));
iRet = recv(sock, fileName, sizeof(fileName), 0);
if (iRet == SOCKET_ERROR)
MessageBox(NULL, "Error receiving filename", 0, 0);
strcpy(TempZip, "C:\\Temp\\");
strcat(TempZip, itoa(fileCounter, tempChar, 10));
strcat(TempZip, ".zip ");
strcpy(tempChar, TempZip);
strcat(TempZip,fileName);
fileCounter++;
HINSTANCE hInst = ShellExecute(0,
"open", // Operation to perform
"c:\\PKzip_Utility\\pkzip.
(LPCTSTR)TempZip,
NULL, // Default directory
SW_HIDE); // Hide the zip screen
strcpy(fileName, tempChar);
while((fin = fopen(fileName,"rb")) == NULL); // if the zip file not exist yet then become NULL
counter = filesize(fin);
char FileSize[128];
itoa(counter, FileSize, 10);
iRet = send(sock, FileSize, sizeof(FileSize), 0);
if (iRet == SOCKET_ERROR)
{
return false;
}
char *ToSend = new char[counter];
fseek(fin, 0, SEEK_SET); //move to begining of file again
fread(ToSend, counter,1,fin);
iRet = send(sock, ToSend, counter, 0);
TotalBytes += iRet;
if (iRet == SOCKET_ERROR)
{
fclose(fin);
closesocket(sock);
int error=WSAGetLastError();
if (error==WSAEMSGSIZE)
return false;
}
//send the actual bytes sent
char total[12];
itoa(TotalBytes, total, 10);
send(sock, total, sizeof(total), 0);
//file transfer is complete, so close the socket and file stream
delete [] ToSend;
fclose(fin);
closesocket(sock);
return true;
}
Thanks.
regards,
Kgc
>> the client receive failed.
What is the WSAGetLastError() code ?
ASKER
Dear fatalXception, mxjijo,
Thanks for reply,
>>Is the client correctly receiving the file size that's being sent from the server?
- sometimes it receive the correct file size, sometimes does not.
>>Have you tried splitting the read part into smaller chunks?
- trying now. I'm sorry cause it is a bit messy.
>> the client receive failed.
What is the WSAGetLastError() code ?
-it's hanged... Did not show any code
I suspect is the problem cause by the server side, that is the server can't zip the selected file in time, and this break the sequence between client and server.
Thanks for reply,
>>Is the client correctly receiving the file size that's being sent from the server?
- sometimes it receive the correct file size, sometimes does not.
>>Have you tried splitting the read part into smaller chunks?
- trying now. I'm sorry cause it is a bit messy.
>> the client receive failed.
What is the WSAGetLastError() code ?
-it's hanged... Did not show any code
I suspect is the problem cause by the server side, that is the server can't zip the selected file in time, and this break the sequence between client and server.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dear fatalXception,
I tried, everythings go fine.
I tried, everythings go fine.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Have you tried splitting the read part into smaller chunks?