Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: socket VC++
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:
 
 
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 22-Jan-12 19:20pm

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 334
2 CPallini 275
3 DamithSL 230
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 23 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100