Skip to main content
gamedev.net
🔒 Locked

[.net] C# Network/Internet programming

Started by Cybrosys Sep 17, 2006 at 7:07 AM 4 replies 3.1k views
Original Post
Cybrosys
Cybrosys
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?
paulecoyote
paulecoyote
Well you can use the Serialization stuff to pass objects via streams. [Serializable] attribute. And then there's remoting too.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Cybrosys
Cybrosys
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.
paulecoyote
paulecoyote
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.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Cybrosys
Cybrosys
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?
paulecoyote
paulecoyote
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.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.