jelly5798
jelly5798

Reputation: 349

Sending information down a socket in C#

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.

Upvotes: 3

Views: 334

Answers (5)

TheCodeMonk
TheCodeMonk

Reputation: 2099

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.

Upvotes: 1

tjmoore
tjmoore

Reputation: 1084

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.

Upvotes: 1

Preet Sangha
Preet Sangha

Reputation: 65555

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.

Upvotes: 0

nothrow
nothrow

Reputation: 16168

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

Upvotes: 0

Dave
Dave

Reputation: 15016

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.

Upvotes: 0

Related Questions