Passing objects with sockets

  • Thread starter Thread starter cold
  • Start date Start date
C

cold

I'm trying to write an helper class that allows me to serialize objects to a
socket (or a NetworkStream). I know there are plenty of .NET classes that
help me to serialize object to an array of byte, but I can't undestand how I
can send a block of bytes and being sure the receiver will receive only this
block....I mean, if I write the bytes to a NetworkStream the receiver will
get a stream of bytes and he won't have any way to know how to "split" the
data. Am I supposed to build a protocol, using a special byte to mark the
beginning and the end of every "message" (in this case I should stuff the
bytes before sending)?

I hope I've explained well what I would like to do

Thank you for your help

Seba
 
well, you could if you want to.
but I think you might be interested by remoting !!

which is the .NET RPC mechanism......
 
You can use either:

- fixed-size messages
- variable-size messages with the size in each message
- a special separator at the end of each message.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
And which separator can I use? I mean, I can't be sure the character I'll
use won't be present in the content of the stream of bytes I'm sending. I
suppose I have to stuff the bytes...any suggestion?

thanks

Seba
 
For binary messages there is no safe separator. For text messages, you can
use some sequence of characters that are not part of your protocol.

In general, it's better to include the size in the message header. It will
perform better than checking each byte to find separators...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
you obviously has to pass the size of the date first, in the message header
so you should serialize to a MemoryStream first, get the byt array, then
construct you message with a simple header, I would say

MSG:
| MSG type (1 byte) | MSG size (4 bytes) | MSG data (your serialized object,
"n" bytes specified in size) |

but why not use remoting?
it's a perfect high level, object oriented, .NET specific, interprocess
communication protocol/tool
 
Back
Top