|
-
June 23rd, 2006, 05:26 PM
#1
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 ?
-
June 23rd, 2006, 06:20 PM
#2
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
-
June 23rd, 2006, 07:11 PM
#3
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.
-
June 23rd, 2006, 07:31 PM
#4
Re: CAsynchSocket::Receive max value ?
 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
-
June 24th, 2006, 04:32 AM
#5
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!
-
June 24th, 2006, 04:52 AM
#6
Re: CAsynchSocket::Receive max value ?
-
June 24th, 2006, 09:24 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|