Sign in to follow this  

[.net] C# Network/Internet programming

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question regarding sockets, tcpclient, udpclient, tcplistener etc. The problem concerns how to send "objects" intead of just strings or such. What i want to do, or is trying to do is simply to define classes/structs and send those as objects. Each class will have a header at the top, the first x num bytes. So that when i'm receiving data i'll always start by reading the first few x bytes and parse them into the message header class/struct so i know what data is in the buffer. First of all, i need to know the size of the message classes or structs, but sizeof doesn't work so i have to get the size some other way(?). Could anyone direct me to information or how i should send information properly using *clients and sockets?

Share this post


Link to post
Share on other sites
Well i know of the Serializable attribute, but the problem of knowing the size of an object still remains.

And would remoting be a wise choice? I haven't done any test with it and just to mention it, the networking will be for a game. Nothing too fancy, just some tcp data transfers for important stuff ie client information and udp for movement packets etc.

Share this post


Link to post
Share on other sites
Well the thing with .Net stuff is that it tries to take care of as much stuff for you as you can. Headers and all that too.

You can query the serializer of your choice for information about bytes and sizes of things.

The thing is having that stuff stored in the class doesn't make much sense, because if you chain together a few serializers (like a xml > gzip > network stream) then the actual size is going to change in each of those states.

Seems to me you are from a C or C++ background right, where you might cast something to a byte array, send that across, and cast it back?

There would be ways of doing that, you can fix a class def like that for interop. But rather try and shoe-horn C++/C thinking in to a C# problem space, I would encourage you to investigate the various strategies MS have set out for you for this stuff. I would suggest letting C# / .Net handle as much as you can, and if performance issue pop up then think about writing your own serializer.

Share this post


Link to post
Share on other sites
Yes, you are correct about the c/c++.

The thing is, what if i serialize two objects and send them across the network...

The TcpClient recieves the two objects, but how should it know what objects it has recieved etc? Will two *.Write calls always be handled on the server/client side as, for example, 2 BeginReadComplete events?

The examples i've found has always involved someone sending a string or reading strings from a file and sending them over the network. I haven't seen any examples of actually sending "objects" over the network. The only example of that is a Remoting example but i'm worried that it's not that performance wise.

Btw, does the TcpClient objects handle disconnection events, client lost etc or do i have to monitor that my self by, for example, ping messages?

Share this post


Link to post
Share on other sites
Although this is far more then what you are asking for, try looking at the WSE stuff

http://msdn.microsoft.com/webservices/webservices/building/wse/default.aspx

There are some workshops there that show different types of serialisation, including putting things through a security pipeline.

Obejcts can very easily be serialised in to strings or byte arrays. Try looking up SoapFormatter and BinaryFormatter in the MSDN. These things can write directly to a writable Stream.

Depending on your requirements, you may find NetworkStream helpful too.

Share this post


Link to post
Share on other sites

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this