Best way to do a connection-oriented Send/Receive.

  • Thread starter Thread starter Simon Says
  • Start date Start date
S

Simon Says

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
 
Simon,

The smartest way is probably using a webservice that collects your data.

(What is very easy to make using VB.Net 2005)

Just my thought,

Cor
 
It sounds good if mine is a web application. However, my is a Windows
application. Will it still works?
--Simon
 
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
 
But with XML data, is there any more efficient way of doing it? I'm looking
at some of the XML functions right now, and am exploring the
'CanDeserialize' function in the XML.Serialization namespace. Thinking of
putting all received message from the socket into a stream, and keep
checking if 'CanDeserialize' return true. If the message is not complete,
then 'CanDeserialize' will return false on the stream.

Any pointers?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top