Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

        // open the file and send the file length.
        if( !( bFileIsOpen = cfSourceFile.Open( csFileName_i, CFile::modeRead | CFile::typeBinary, 0 )))
        {
            return false;
        }

        nFileLength = ( int )cfSourceFile.GetLength();
        /*nFileLength = htonl( nFileLength );*/
        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
            {
                //SMB_LOGERROR( SMB_SOCKET_DATA_SEND_FAIL );
                WSAGetLastError();
                return false;
            }
        }
        while ( nLengthToSend > 0 );

        // sending the file content
        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;
                }// If socket error
                else
                {
                    //SMB_LOGERROR( SMB_SOCKET_DATA_SEND_FAIL );
                    WSAGetLastError();
                    return false;
                }
            }
            while ( nSendBytes > 0 );
            csLog.Format( _T("bytes to send %d"), nLengthToSend );
            OutputDebugString( csLog );
        }
        while ( nLengthToSend > 0 );
        cfSourceFile.Close();
    }


Server Side:

C++
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;
        }
        // receiving file length.
        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 );
        //nLength = ntohl( nLength );
        nLengthToReceive = nLength;

        // receiving file content.
        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;  // will be allocated and filled by FormatMessage

                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;
            }// if SOCKET_ERROR
            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 !! :(
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900