[.net] Good network API for games?

Started by
8 comments, last by clooge 17 years, 6 months ago
Hi, I'm just wondering if there are any good network APIs for C#? I've tried googling but haven't found anything solid yet. It should be suitable for use in a game, so it has to be pretty fast etc. Do you have any tips or is WinSock the way to go?

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

Advertisement
You can use the UdpClient class to send bytes directly from one point to another (the TcpClient class is similar, but with TCP). Another way of doing things is to wrap a BinaryWriter around a NetworkStream and write a series of primitives to the stream (String, Int32, Boolean, etc.). A BinaryReader wrapped around another NetworkStream on the other side will pick up the primitives without you having to any of the serialization/deserialization.

I found a CodeProject article about a simple game that uses the BinaryReader/BinaryWriter technique.
Shameless plug ... relatively low level packetised TCP. The built in UDPClient/TCPClient are quite reasonable on their own, too.
Typically, when you say "network API for games" you mean something that provides a bit more structure. Like perhaps a string compression system, or an object property replication system, or reliable channels over UDP or simlar useful functions. The built-in endpoint classes don't do that; they're just shallow wrappers on top of sockets.
enum Bool { True, False, FileNotFound };

I'm looking for something probably similar...I'm doing a Pong game in C# to play On-line between two hosts and I am searching for a library in C# that help me with all the messages between the hosts, specially about the syncronization between them, leaving old messages, etc... Do you know something to help me?

Thanks
Lidgren.Library.Network might be worth looking into if you want. I ran acrossed it the other day.
"Knowledge is of two kinds. We know a subject ourselves, or we know where we can find information upon it." - Samuel Johnson
Cool! Thanks for the tips, I'll check them out immediately!

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

Can't foget this one http://rakkarsoft.com/
If you are just prototyping the problem out, or you aren't expecting huge amounts of communication then you should just use .Net Remoting. It will marshal all of the parameters in your method calls across the network for you.

If you are interested in more performance, or are interested in how to make a network library like this then... (otherwise, just skip this post)

Here is a general overview of a couple of the details in creating your own.
You should create tcp clients and servers on both ends.

Create a dictionary of the kinds of datatypes that you are sending back and forth.
The dictionary is composed of op-codes.

public const int opCode_Integer = 1;
public const int opCode_String = 2;

//The datatypes can be simple or complex
public const int opCode_PlayerInfo = 3; //(integer, integer, integer, string)

//strings can be stored in pascal form
//first byte contains length of string up to 255
//each following byte contains a char in the string
// ie.) 3abc

//integers are stored in 8 bytes -- might use the convert class to help here

public void SendData(int opcode, object data)
{
byte[] packet;

//pack object data into packet
switch (opcode)
{
...
}

//open a socket and send packet accordingly.
}

public byte[] RecieveData(int opcode);
{
//Listen for an opcode to come in
//if timeout then request resend (if possible)
}
Another method is using queues (I have it working upto 3500 messages per/sec, if you turn of journaling in the MSMQ queues you may get more)

you can use the class below I listed and hand off any object to it and call the send method.

The only thing tell it the path of the queue you are using.
then
from your code two call's only
1-serialize()
2-send()





/// <summary>
/// Summary description for GenericSerialization.
/// </summary>
public class GenericSerialization
{
public GenericSerialization()
{
}

public static string Serialize(object objToSend, System.Type objType)
{
StringWriter strWriter = null;
string strXmlSerializedObject = null;

try
{
XmlSerializer serializer = new XmlSerializer(objType);

StringBuilder sb = new StringBuilder();
strWriter = new StringWriter(sb);
serializer.Serialize(strWriter, objToSend);
strWriter.Flush();

strXmlSerializedObject = sb.ToString();
}
catch (Exception e)
{
StringBuilder s = new StringBuilder("Generic.SerializeCommand() failed.\r\n");
s.AppendFormat("Object being serialized = {0}\r\n", objType.ToString());
s.AppendFormat("Exception info: {0}", e.Message);
Trace.WriteLineIf(Globals.myglobalswitch.TraceError, s.ToString());
}
finally
{
if (strWriter != null)
strWriter.Close();
}

return strXmlSerializedObject;
}

public static object Deserialize(string strXmlToDeserialize, System.Type objType)
{
try
{
XmlSerializer serializer = new XmlSerializer(objType);
return serializer.Deserialize(new StringReader(strXmlToDeserialize));
}
catch
{
Trace.WriteLineIf(Globals.myglobalswitch.TraceError, "Exception occured while trying to deserialize the following: " + strXmlToDeserialize);
}

return null;
}

}

This topic is closed to new replies.

Advertisement