471,616 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Passing objects with sockets

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
Jul 21 '05 #1
5 1459
well, you could if you want to.
but I think you might be interested by remoting !!

which is the .NET RPC mechanism......
"cold" <co****@libero.it> wrote in message
news:e1**************@TK2MSFTNGP14.phx.gbl...
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

Jul 21 '05 #2
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

"cold" <co****@libero.it> escribió en el mensaje
news:e1**************@TK2MSFTNGP14.phx.gbl...
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

Jul 21 '05 #3
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

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> ha scritto nel
messaggio news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"cold" <co****@libero.it> escribió en el mensaje
news:e1**************@TK2MSFTNGP14.phx.gbl...
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


Jul 21 '05 #4
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

"cold" <co****@libero.it> escribió en el mensaje
news:%2***************@TK2MSFTNGP09.phx.gbl...
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


Jul 21 '05 #5
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

"cold" <co****@libero.it> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> ha scritto
nel messaggio news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"cold" <co****@libero.it> escribió en el mensaje
news:e1**************@TK2MSFTNGP14.phx.gbl...
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



Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan | last post: by
11 posts views Thread by Mike M | last post: by
3 posts views Thread by domeceo | last post: by
6 posts views Thread by Scott Zabolotzky | last post: by
1 post views Thread by cold | last post: by
12 posts views Thread by manochavishal | last post: by
2 posts views Thread by william.w.oneill | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.