Hi Simon,
There are three common approaches to know when a message is complete:
- Use a delimiter (your approach)
- Use a message header (2 bytes, for example) to inform about of the length
of the message.
- Use fixed-length messages
With either approach you will need to loop in the receiving code until the
message is complete, because there are several buffers involved.
--
Best regards,
Carlos J. Quintero
MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Simon Says" <(E-Mail Removed)> escribió en el mensaje
news:%(E-Mail Removed)...
> Hi all,
>
> I've a message object that is serialized to a xml string and communicating
> between a client and server with a connection-oriented (TCP) socket.
> Unlike a UDP, in a TCP socket, the 'Receive' function will try to read in
> as much data as possible from the buffer and return, and therefore,
> sometimes the message isn't complete with just one 'Receive' call.
>
> In order to make sure that my message is complete, I've added in a 'STX'
> and 'EOT' into my xml string just to make sure that whether if more than 1
> 'Receive' call is needed to complete the message. It definitely works, but
> I'm just wondering if there is any other smarter way of doing it? Is it
> possible do it like the UDP's 'receivefrom' function where it will only
> return when the whole datagram is received? I'm using VS2005 now, any new
> tricks?
>
> Thanks,
> Simon
>