views:

58

answers:

2

What would be the best way to continuously send the mouse position to another computer in C#?

So far I use a TCP socket for transportation. I get the ToString() of MousePosition, convert it to byte[] and send that byte array. On the receiving side I append the bytes to a StringBuilder, parse it and construct a new point. At least it works :-)

I'm new to .net (though I know some Java) and I think there is a better way.

Can one serialize / deserialize a System.Drawing.Point in some (elegant) way? Can one send this serialized object to a remote machine? And if so, how?

Regards
Mike
[;-)

+2  A: 

Don't bother with either serialization or strings: just send down the pair of coordinates as integers. You can use BitConverter to convert integers into bytes - or just use BinaryWriter for writing and BinaryReader for reading.

Jon Skeet
+1  A: 

System.Drawing.Point is already marked with <Serializable>, so if you want to, you can use .NET's built in serialization.

Cade Roux
Yep. So is it a good idea to instantiate a BinaryFormatter and a MemoryStream with some arbitrary sized byte array to send the points bytes over and over again? (Reusing the Formatter, of course)
DerMike
@DerMike, only if you are immediately planning on sending bigger structures. I think I'm with Jon Skeet on this, you can always start with writing the points as integers with binary writer and later move to full serialization as you start to have a whole structure (timestamps, keyboard states, etc.)
Cade Roux