Hi,
i have a program that sends a file from the client to the server via Sockets.
But the file transfered is always corrupt as the recv function hangs at the end of the file.
Client Side
<pre lang="c++">
OutputDebugString( L"In the send function" );
int nFileLength;
int nLengthToSend;
char* cSendData;
CFile cfSourceFile;
BOOL bFileIsOpen = FALSE;
if( !( bFileIsOpen = cfSourceFile.Open( csFileName_i, CFile::modeRead | CFile::typeBinary, 0 )))
{
return false;
}
nFileLength = ( int )cfSourceFile.GetLength();
nLengthToSend = nFileLength;
CString csSize;
csSize.Format( _T("%d"), nFileLength );
nLengthToSend = csSize.GetLength();
char *pBuffer = new char[ nLengthToSend + 1 ];
for( int i = 0; i < nLengthToSend; ++i )
{
pBuffer[ i ] = csSize[ i ];
}
do
{
int nBytesSent;
int nlength = atoi( pBuffer );
CString csTest;
csTest.Format( _T("%d"), nlength );
OutputDebugString( csTest );
nBytesSent = send( sSocketID_i, pBuffer, nLengthToSend, 0 );
if( SOCKET_ERROR != nBytesSent )
{
nLengthToSend -= nBytesSent;
}
else
{
WSAGetLastError();
return false;
}
}
while ( nLengthToSend > 0 );
nLengthToSend = nFileLength;
cSendData= new char[ BUFFER_SIZE ];
memset( cSendData, 0, BUFFER_SIZE );
int nDataSentNow;
int nSendBytes;
int buffOffset;
CString csLog;
do
{
int nGetLength = ( nLengthToSend < BUFFER_SIZE ) ? nLengthToSend : BUFFER_SIZE;
nSendBytes = cfSourceFile.Read( cSendData, nGetLength );
buffOffset = 0;
do
{
nDataSentNow = send( sSocketID_i, static_cast<const char*>( cSendData + buffOffset ), nSendBytes, 0 );
if( SOCKET_ERROR != nDataSentNow )
{
buffOffset += nDataSentNow;
nSendBytes -= nDataSentNow;
nLengthToSend -= nDataSentNow;
}
else
{
WSAGetLastError();
return false;
}
}
while ( nSendBytes > 0 );
csLog.Format( _T("bytes to send %d"), nLengthToSend );
OutputDebugString( csLog );
}
while ( nLengthToSend > 0 );
cfSourceFile.Close();
}
Server Side:
int nError;
int nDataLength;
int nBytesRead;
int nLengthToReceive;
CFile cfDestFile;
CFileException fExpFile;
BOOL bFileIsOpen = FALSE;
CString csFileName;
int nSize = 0;
nLengthToReceive = sizeof( nDataLength );
if( !( bFileIsOpen = cfDestFile.Open( csFileName_i, CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary, &fExpFile )))
{
nError = WSAGetLastError();
return false;
}
char cFile[ 513 ];
memset( cFile, 0, 512 );
nBytesRead = recv( sSocketID_i, cFile, 512, 0 );
int nLength;
cFile[ ++nBytesRead ] = '\0';
nLength = atoi(cFile);
int nRecvdData;
char* cReadData = new char[BUFFER_SIZE];
memset( cReadData, 0, BUFFER_SIZE );
nLengthToReceive = nLength;
int nGetLength;
TCHAR tszTemp[ MAX_PATH ] = { 0 };
do
{
nGetLength = ( nLengthToReceive < BUFFER_SIZE ) ? nLengthToReceive : BUFFER_SIZE;
nRecvdData = recv( sSocketID_i, cReadData, nGetLength, 0 );
swprintf( tszTemp, L"#ARC Recived Data = %d", nRecvdData );
OutputDebugString( tszTemp );
if( SOCKET_ERROR == nRecvdData )
{
int errCode = WSAGetLastError();
LPSTR errString = NULL;
int size = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, 0, errCode, 0,(LPWSTR)&errString, 0, 0 );
OutputDebugString( (LPCWSTR)errString );
LocalFree( errString ) ;
return false;
} cfDestFile.Write( cReadData, nRecvdData );
nLengthToReceive -= nRecvdData;
swprintf( tszTemp, L"#ARC nLengthToReceive = %d", nLengthToReceive );
OutputDebugString( tszTemp );
}
while ( nLengthToReceive > 0 );
OutputDebugString( L"To close file" );
cfDestFile.Close();
please Help !! :(