Solved

File Transfer

Posted on 2006-07-18
3
246 Views
Last Modified: 2013-11-20
Hi
I am sending files from ona pda to other, i have problem in the client side, the code worked fine but after a day it is not working, i am getting an error sending the file size(Error2 in the code), can anyone tell how to solve this problem and also a better way to trouble shooting the errors.

BOOL CFinalVersionDlg::OnBreceive()
{

    /***************************
    // connects to a remote server and downloads a file from it
    // the remote server must be running a counterpart
    //    CString fName = name of local file to which
    //    downlaoded data will be stored
    // Output: BOOL return value indiactes success or failure of the download
    ****************************/

    // create client socket and connect to server
   
    ///    AfxSocketInit(NULL);
    // make certain this is done somewhere in each thread
    // (usually in InitInstance for main thread)
    CSocket sockClient;
    sockClient.Create();

   // PRE_AGREED_PORT is #define'd as 8686
      CString strIP="10.10.10.2";
   sockClient.Connect( strIP, PRE_AGREED_PORT );
   
   
    // local variables used in file transfer (declared here to avoid
    // "goto skips definition"-style compiler errors)
   
    BOOL bRet = TRUE; // return value
    // used to monitor the progress of a receive operation
    int dataLength, cbBytesRet, cbLeftToReceive;
    // pointer to buffer for receiving data
    // (memory is allocated after obtaining file size)
    BYTE* recdData = NULL;
   
    CFile destFile;
    CFileException fe;
    BOOL bFileIsOpen = FALSE;
   
    // open/create target file that receives the transferred data
    TCHAR* pathName = _T("hi.txt");
    if( !( bFileIsOpen = destFile.Open( pathName, CFile::modeCreate |
           CFile::modeWrite | CFile::typeBinary, &fe ) ) )
    {
        TCHAR strCause[256];
        fe.GetErrorMessage( strCause, 255 );
        MessageBox(TEXT("Error1"),MB_OK);
        /* you should handle the error here */
       
        bRet = FALSE;
        goto PreReturnCleanup;
    }
   
   
    // get the file's size first
    cbLeftToReceive = sizeof( dataLength );
   
    do
    {
        BYTE* bp = (BYTE*)(&dataLength) + sizeof(dataLength) - cbLeftToReceive;
        cbBytesRet = sockClient.Receive( bp, cbLeftToReceive );
       
        // test for errors and get out if they occurred
        if ( cbBytesRet == SOCKET_ERROR || cbBytesRet == 0 )
        {
            int iErr = ::GetLastError();
           MessageBox(TEXT("Error2"),MB_OK);
            /* you should handle the error here */
           
            bRet = FALSE;
            goto PreReturnCleanup;
        }
       
        // good data was retrieved, so accumulate
        // it with already-received data
        cbLeftToReceive -= cbBytesRet;
       
    }
    while ( cbLeftToReceive > 0 );

    dataLength = ntohl( dataLength );

    // now get the file in RECV_BUFFER_SIZE chunks at a time
   
    recdData = new byte[RECV_BUFFER_SIZE];
    cbLeftToReceive = dataLength;
   
    do
    {    
        int iiGet, iiRecd;

        iiGet = (cbLeftToReceive<RECV_BUFFER_SIZE) ?
                      cbLeftToReceive : RECV_BUFFER_SIZE ;
        iiRecd = sockClient.Receive( recdData, iiGet );

        // test for errors and get out if they occurred
        if ( iiRecd == SOCKET_ERROR || iiRecd == 0 )
        {
            int iErr = ::GetLastError();
            MessageBox(TEXT("Error3"),MB_OK);
           
            /* you should handle the error here */
           
            bRet = FALSE;
            goto PreReturnCleanup;
        }

        // good data was retrieved, so accumulate
        // it with already-received data
       
        destFile.Write( recdData, iiRecd); // Write it
        cbLeftToReceive -= iiRecd;

    }
    while ( cbLeftToReceive > 0 );

      MessageBox(TEXT("File Downloaded"),MB_OK);

PreReturnCleanup: // labelled "goto" destination
   
    // free allocated memory
    // if we got here from a goto that skipped allocation,
    // delete of NULL pointer
    // is permissible under C++ standard and is harmless
    delete[] recdData;        
   
    if ( bFileIsOpen )
        destFile.Close();
        // only close file if it's open (open might have failed above)
   
    sockClient.Close();
   
    return bRet;
}      
0
Comment
Question by:Prakashv20
  • 3
3 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17136196
You have not initialized 'dataLength' parameter anywhere ? I can see you have directly used dataLength after declaration...also I have not seen you have retrieved File Size for destFile anywhere ???....

 // get the file's size first
    cbLeftToReceive = sizeof( dataLength ); <== ?

and File Size can be obtained using CFile::GetLength() function......

e.g
int nFileSize = destFile.GetLength(); <== this will retrieve file size for destFile and will assign to nFileSize..

-MAHESH
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 250 total points
ID: 17136211
So I think if you want file size for destFile in dataLength your code should be look like :


   dataLength = destFile.GetLength()  // get the file's size first

   cbLeftToReceive = dataLength;    // Note to assign size of file which is present in dataLength to initially assign to cbLeftToReceive ( and NOT size of dataLength )

  // Proceed with file receive loop


-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17158778
so prakash how you solved your problem ?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fizzArray  challenge 1 96
isEverywhere  challenge 19 82
NotAlone Challenge 20 84
Problem to App source 6 72
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question