views:

67

answers:

5

I have built two programs in C# and I am sending simple strings through the sockets. This is fine for the moment but in the near future I will need to send more complicated items, such as objects down the sockets and eventually files.

What steps would I take to do this? What purpose do the buffers serve for the sockets/streams? Apologies if I am a little vague.

A: 

One way you can handle this is to serialize your object into XML, send over the socket, then deserialize it. I've done it this way before. However, I (being fairly new to .NET) just learned about the JavaScriptSerializer, which I believe makes this process a lot easier for you.

Dave
A: 

You need to serialize the objects.. Mark it with [Serializable] attribute and use some serializers.. Example can be found here.

Yossarian
A: 

First thing in any comms situation is to consider that anything you send must be able to get serialised and de serialised so that it can get over a comms channel. Next you must consider that comms have latency (its not instantaneous), and then the fact that it can fail.

After this you consider the protocols and technology to enable the above to be factored in.

Preet Sangha
+1  A: 

There are many options, but basically you want to serialise the data into a format that will go through the socket.

Worth looking here into xml serialisation.

tjmoore
A: 

If you are sending objects, you have to really be careful with what you do and how you are planning on using those objects on the other end. All properties need to be serialized. If you are going to have large amounts of data in theses objects, you may want to use binary serialization instead.

Also, look at the guidelines posted here: MSDN Serialization Guidelines

If you are going to be sending objects, you may want to look at either .Net Remoting options or WCF Services if applicable. Rolling your own socket handlers and then using it for complex operations is asking for a lot of time and pain, especially if you haven't done it before.

TheCodeMonk
Thanks for everyone's answers. Very helpful though the link provided by TheCodeMonk was great. Cheers.
markyoung1984