Sending struct over network
by Chris Kim · in Torque X 2D · 04/18/2007 (11:36 am) · 3 replies
I'm new to C# and I'm doing an experiment sending a packet over the network since TorqueX does not support network yet. ^^
I got the socket to work but my problem is that sending a simple structure over socket is not as obvious as it seems.
I tried to look up on internet but I don't want "serialization" as it send bunch of XML craps as well.
What I want is just to send packed binary data.
An example structure is defined,
public struct Packet
{
int command;
int length;
float timeStamp;
string message;
}
In C/C++, it can be done pretty easy by type casting to char* and send it as byte stream.
How can this be done in C#? to send and to receive from both sides?
Boxing/Unboxing to byte[] doesn't seem to work.
I apprecite for your help.
I got the socket to work but my problem is that sending a simple structure over socket is not as obvious as it seems.
I tried to look up on internet but I don't want "serialization" as it send bunch of XML craps as well.
What I want is just to send packed binary data.
An example structure is defined,
public struct Packet
{
int command;
int length;
float timeStamp;
string message;
}
In C/C++, it can be done pretty easy by type casting to char* and send it as byte stream.
How can this be done in C#? to send and to receive from both sides?
Boxing/Unboxing to byte[] doesn't seem to work.
I apprecite for your help.
About the author
#2
Matias
04/21/2007 (5:50 am)
You might want to check out Lidgren Network Library, which is a .NET networking library for TCP and UDP communications. It abstracts the the transport layer (sockets, connections, handshakes, etc.) and allows you to just send & receive normal C# data types. Documentation is a bit slim, but there's a quick start and an example chat application that should get you started. It's under BSD-license and comes with the source.Matias
#3
Got my information from Krishna ( http://xnarocks.spaces.live.com/ ) when Microsoft and UMBC had him in to talk about XNA.
04/21/2007 (3:51 pm)
Since XNA 1.5 will have netcode (and allegedly be out before the end of 2007) you might want to do all of your network stuff through an abstraction layer. That way when XNA gets netcode you can simple change the low level socket and have your netcode working on the Xbox as well.Got my information from Krishna ( http://xnarocks.spaces.live.com/ ) when Microsoft and UMBC had him in to talk about XNA.
Torque Owner Jason Swearingen
note, that you will have to spend a few hours learning first how serialization works, and second learning how to use webservices.
msft's MSDN online is a fantastic place to start.