Re: CAsyncSocket::Reveive() problem
"nexolite" <nexolite@discussions.microsoft.com> wrote in message
news:47AD1B40-9CBD-4BC1-B196-B2A26EA1C908@microsoft.com...
I am receiving some data in my application through the Receive() function
TCHAR data[100];
int read=serv1.Receive(data,100);
so here the data received in the data buffer is always half the data sent
from the other side
like if i send "AAAAAAAA" then i receive only "AAAA" which is half of the
sent.
what is the problem?
This is newbie question number 1 about sockets.
Sockets provide a 'stream' connection, which has no data length associated
with it. The number of bytes returned by Receive is completely
unpredictable - it depends on the network and timing coincidences, and it is
independent of the number of bytes sent in one Send call. You must
determine, somehow based on your known message structure, when you have
received a complete message. When you don't yet have enough just return and
wait for more data to arrive. Make no assumptions about the number of bytes
you will receive on any particular Receive call.
Some people put a byte count at the front of each message. If the messages
are characters you might use a nul or '\n' at the end of each message.
You've got to do something to allow you to reassemble messages from the
arbitrary-length fragments you receive.
--
Scott McPhillips [VC++ MVP]