Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How would I be able to send a class through the internet like this.

class Player
{
    public int Health;
    public Player Copy()
    {
        return this;
    }
}

And do this.

SendPacket(new Player);

Well something like that?

share|improve this question

closed as off topic by Jonathan Hobbs, ashes999, Josh Petrie, Byte56, Ricket Nov 12 '12 at 22:01

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Look up serialization. –  Jonathan Hobbs Nov 12 '12 at 10:10
1  
Doh I didn't realize I was on the gamedev section. This question would be a better fit for stackexchange.com since it is a general programming question not a game related question. However I can't flag it for move (has that been changed?). –  Roy T. Nov 12 '12 at 10:17
    
@RoyT Pretty sure those kinds of flags just turn into close votes now. –  Jonathan Hobbs Nov 12 '12 at 10:37
add comment

1 Answer

You have to do two things.

First you have to be able to serialize your object. The definition, according to MSDN is:

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

You can find many ways to do this in C# but I would recommend to use the built in binary serialization techniques in .NET. You can start reading up on that here http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx

Then you have to make some sort of connection. Between two computers. One way to do this is using sockets. Microsoft has made this quite easy in .NET (although I think in Java it is slightly easier but less flexible). To keep it simple I would suggest using Synchronous Sockets first. Synchronous means that the program 'hangs' until all the data has been received by the other party. See http://msdn.microsoft.com/en-us/library/6xt5x5zw.aspx

If you don't want the program to hang while sending the data you can use Asynchronous Sockets. They are a lot harder to use because you have to explicitly tell the other end how much data to expect and you have to handle the several steps the sending takes. I found the MSDN tutorial on this a bit unclear but you can find them for client and server here and here. I also wrote my own tutorial which you can find here and if you try Google you will see there are a lot more so skim a few and then pick the one that you find explains it best.

Recap

  1. Serialize the object
  2. Send the bytes using a socket
  3. Receive the bytes on the other end
  4. Deserialize and we have reconstructed the object on another computer :)
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.