CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2006
    Posts
    46

    CAsynchSocket::Receive max value ?

    Hi, I am using CAsynchSocket derived class to send and receive data.

    sendResult = socket.Send( charBuffer, bufferLength );

    receiveResult = socket.Receive( charBuffer, bufferLength );

    Everything works fine until bufferLength exceeds a value of 8760 bytes ( or chars ). In such scenario, say sending buffer 12000 chars, sendResult = 12000, but receiveResult = 8760. Below this value ( 8760 bytes), sendResult and receiveResult are always equal. Both client and server run on the same machine.

    Has anyone encountered similar problem ?

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: CAsynchSocket::Receive max value ?

    This behavior is "by design". TCP sockets are stream-based, not "packet" or "message" based. There is no such thing as a "message" in TCP; there's only a stream of bytes, and Receive might stop receiving anywhere between the beginning and the end of the stream.

    As a consequence, you must program with the expectation that any of the following can happen:
    - multiple calls to Send are grouped together and are retrieved in a single call to Receive
    - one call to Send is broken apart and all of the sent data can only be retrieved by multiple calls to Receive
    - one call to Send does not result in all data being sent, and you must call Send again to send the remainder

    So, what you are seeing is perfectly normal. You sent 12000. Your first call to Receive retrieves the first 8760 bytes. Call Receive again (and again and agian if necessary) until you retrieve all your data.

    Mike

  3. #3
    Join Date
    Jun 2006
    Posts
    46

    Re: CAsynchSocket::Receive max value ?

    I did not know that "Send "and "Receive" behaviour is so unpredictable, however I used repeated "Send" on operation would block error when sending a huge file 700MB. Using buffer = 4096, every single "Send" is followed by a single "Receive" and surprisingly it works without repeating any of them ( apart from mentioned "operation would block error". To be clear, every sendResult = 4096 and every receiveResult = 4096 as well, with any size file.
    This lucky scenario works when both client and server run on the same machine, perhaps it would fail on a real network ?
    Thank you, MikeAThon, tomorrow I will modify my program according to your hints and post results.

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: CAsynchSocket::Receive max value ?

    Quote Originally Posted by Cielak
    ...This lucky scenario works when both client and server run on the same machine, perhaps it would fail on a real network ?
    It was definitely luck, and it definitely would fail on a real network.

    Maybe this article would help: "Network Transfer Of Files Using MFC's CSocket Class" http://www.codeproject.com/internet/...leTransfer.asp . Read the explanation for socket communications.

    Note that one consequence of this behavior is that the recipient will not generally know when he has received all the data that the sender has sent. A solution (which was adopted in the article I cited) is for the sender to pre-pend the length of the data transmission in front of the actual data transmission. That way, the recipient can know right away (since it's at the front) how much more data he can expect.

    There are other solutions, all generally referred to as a "protocol" or "packet scheme".

    Good luck coding the revisions!

    Mike

  5. #5
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: CAsynchSocket::Receive max value ?

    This should really be put sticky in the network forum with a new striking title, because it is a common mistake.
    Please don't forget to rate users who helped you!

  6. #6
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: CAsynchSocket::Receive max value ?

    [ Moved Thread ]

  7. #7
    Join Date
    Jun 2006
    Posts
    46

    Re: CAsynchSocket::Receive max value ?

    Ooops, I did not know that mfc network issues must be posted in this category. Now I know...
    Thanks Mike again, that article turned to be a great help. Now I need to completely redesign everything :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured