Object Transfer Over Network ( Socket )

  • Thread starter Thread starter rawCoder
  • Start date Start date
R

rawCoder

Hi All,

I need some advanced samples or references for passing custom objects over
the network using sockets.
Without using Remoting what are other options in .NET Framework for this
binary serialization.

Thank You
rawCoder
 
rawCoder,

Basically, you have to use serialization. You will want to serialize
your object to a MemoryStream (or some other stream, but MemoryStream will
return a byte array to you, which will help you later). Once you have the
stream, get the bytes from it (with a MemoryStream you would call the
ToArray method). From there, just pass the bytes to the Send method on the
socket, and you should be fine.

You just have to read the bytes into a MemoryStream (or some other
stream on the other end) and then pass that stream to a formatter for
deserialization.

The things you have to look out for are the length of the message.
Since not all objects serialized into a fixed-length format, you need to
send a prefix over the socket indicating how long the byte stream for the
object will be. Then, on the other side, you will know how many bytes you
need to read to get the contents of the object.

Also, both sides have to have a reference to the assembly containing the
type.

Hope this helps.
 
rawCoder,

I do not know if he sees this thread himself it has alreaydy a lot of
answers. Personally I find this sample from Tom very nice.

\\\by Tom Shelton
Private Function SerializeFontObject(ByVal fnt As Font) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream
Try
bf.Serialize(mem, fnt)
Return Convert.ToBase64String(mem.ToArray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function
///
\\\
Private Function DeserializeFontObject(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Convert.FromBase64String(fnt))
Try
Return DirectCast(bf.Deserialize(mem), Font)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try
End Function
///

I hope this helps a little bit?

Cor
 
Thank You All for your suggestions.

This information is quite enough for me to get started.
I do not know if he sees this thread himself it has alreaydy a lot of
answers.
Cor , did you mean Me here ? :-) If yes then yes I do see my threads ,
infact have them "Watched" in my OE. I actually dont post unless I feel it
very necessary.

Thank You
rawCoder
 
Cor , did you mean Me here ? :-) If yes then yes I do see my threads ,
infact have them "Watched" in my OE. I actually dont post unless I feel it
very necessary.

No I mean Tom Shelton, the sample I sent is from Tom.
You are not with those were I have slight doubts about that.

:-)

Cor
 
'Serializing variable length string across app domains'

Hi All,

Well I looked into the serialization stuff and seems that the default
serialization of .NEt isnt good enuf for me due to space problem.

the one that suits me most is with the help of Marshal.StructureToPtr
Its concise and good but there is one problem.

In case of unknow length strings , this will not work when the serialization
and de-serialization is done across app domains - as in case of socket apps.
see the structure at the bottom

If there is no direct solution, then I will have to do ALL the serialization
by myself without help from .NET :'-(
Then I will actually not be sending the original objects over network,
rather will be getting the fields out and putting in a generic message with
message header to identify type of message and length and will be then
sending it. Any help in that case will also be appreciated

btw every serialization works very fine in same app domains , but in case of
diff. app domains - things fall apart.

Any suggestions

Thank you in advance
rawCoder

Public Structure STData

Dim a As Integer

'<MarshalAs(UnmanagedType.BStr)> _

Dim b As String

End Structure
 
rawCoder said:
'Serializing variable length string across app domains'

Hi All,

Well I looked into the serialization stuff and seems that the default
serialization of .NEt isnt good enuf for me due to space problem.

the one that suits me most is with the help of Marshal.StructureToPtr
Its concise and good but there is one problem.

In case of unknow length strings , this will not work when the serialization
and de-serialization is done across app domains - as in case of socket apps.
see the structure at the bottom

If there is no direct solution, then I will have to do ALL the serialization
by myself without help from .NET :'-(
Then I will actually not be sending the original objects over network,
rather will be getting the fields out and putting in a generic message with
message header to identify type of message and length and will be then
sending it. Any help in that case will also be appreciated

In reality, it is impossible to send objects anywhere. They only exist in
memory. As far as applications are concerned, they only exist in the
virutal address space of the process in which they were created. Any idea of
object transfer is going to consist of creating a new object that is the
same type as your source object at your destination location and setting its
state to that of your source object.

I am actually in the same boat as you are. I need a PocketPC to talk to a
windows service written in C#. Even if I use the .Net Compact Frame work on
the PocketPC there is no remoting or serialization available. There is also
no DCOM for PocketPC.

Some of the things I am thinking about are:

1. Is it too much work to setup a generic way to remote object over tcp/ip
(probably)
2. Is it too much work to remote a specific type of object over tcp/ip
(probably not)
a. Create a proxy object
b. Create a stub object
c. Create a specific protocol that will allow me to mirror object state
and perform remote method invocation.
3. Am I better off creating an application specific protocol to expose a set
of services over tcp/ip (perhaps).
 

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