Jump to content
  • Advertisement
Sign in to follow this  
BauerGL

[.net] Good network API for games?

This topic is 4422 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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)
}

Share this post


Link to post
Share on other sites
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;
}

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!