Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3278144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:26:54+00:00 2026-05-17T19:26:54+00:00

http://www.codeproject.com/KB/IP/SocketFileTransfer.aspx?artkw=socket%20send%20a%20file I don’t clearly understand this line : // get the file’s size first

  • 0

http://www.codeproject.com/KB/IP/SocketFileTransfer.aspx?artkw=socket%20send%20a%20file
I don’t clearly understand this line :
// 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();
        TRACE( "GetFileFromRemoteSite returned 
          a socket error while getting file length\n"
          "\tNumber of Bytes received (zero means connection was closed) = %d\n"
          "\tGetLastError = %d\n", cbBytesRet, iErr );

        /* 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 );

I want to know how get it get size of the file to dataLength 🙂

This line : BYTE* bp = (BYTE*)(&dataLength) + sizeof(dataLength) - cbLeftToReceive; does it right that bp is a byte pointer of dataLength addr but what + sizeof(dataLength) - cbLeftToReceive mean ?

I don’t think the file is that small : 4 bytes, just onc Receive how can they receive dataLength and data ? Does it send dataLength first and data after ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:26:55+00:00Added an answer on May 17, 2026 at 7:26 pm

    Oh. The funny array arithmetic. The idea is to count from the end, so that when you reach the end you know you’re done. In pieces:
    1. Find the address of dataLength (BYTE*)(&dataLength)
    2. Skip to the end of dataLength + sizeof(dataLength)
    3. Back up by the number of bytes we expect to receive - cbLeftToReceive
    This is where we are writing the bytes we get from the network.

    As we get bytes from the network, we reduce cbLeftToReceive (cbLeftToReceive -= cbBytesRet;) and continue trying to receive bytes until we are done. So every time through the loop, bp points to where we need to write the next bytes we Receive().

    EDIT:

    So now that we know how many bytes we’re going to get, how to we receive them without potentially filling all of RAM with hunks of the data? We get a buffer, repeatedly fill it, and flush that buffer to disk whenever it’s not empty. When there’s still a lot of data (more than a buffer) left to receive, we try to Receive() a fill buffer. When there’s less than a full buffer to go, we only request to the end of the file.

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

    The we catch and handle errors. If there was no error, write however many bytes we got and reduce the number of bytes we expect to receive by the number we got.

      destFile.Write( recdData, iiRecd); // Write it
      cbLeftToReceive -= iiRecd;
    

    If we’re still not done receiving bytes, go back to the top and keep going.

      while ( cbLeftToReceive > 0 );
    

    General advice:
    It’s good to practice reading code where you don’t pay too much attention to the error handling and exception handling code. Typically what’s left is much easier to understand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how variants are implemented, and reading: http://www.codeproject.com/KB/cpp/TTLTyplist.aspx And I'm getting
FastDelegate refers to http://www.codeproject.com/KB/cpp/FastDelegate.aspx , but I don't think it is related. I have
H Regarding this URL http://www.codeproject.com/KB/aspnet/FlashUpload.aspx User.Identity as System.Web.Security.FormsIdentity is always null, because the Identity
Please refer to the topic http://www.codeproject.com/KB/viewstate/SaveViewState.aspx . The topic demonstrates how you can save
I am looking at this tutorial http://www.codeproject.com/KB/cpp/authforwebservices.aspx and I am wondering what the reason
I'm using the .NET TWAIN code from http://www.codeproject.com/KB/dotnet/twaindotnet.aspx?msg=1007385#xx1007385xx in my application. When I try
I am building CLR Stored Procedures and UDFs as discussed in this article: http://www.codeproject.com/KB/cs/CLR_Stored_Procedure.aspx
Is there a free DayView Calendar for WPF like this one? http://www.codeproject.com/KB/selection/Calendardayview.aspx Thanks in
when i search for open source for domain checker, i got this reference http://www.codeproject.com/KB/aspnet/DataScraping.aspx
I found a very interesting memory leak detector by using Visual C++. http://www.codeproject.com/KB/applications/visualleakdetector.aspx I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.