Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C#

Genesis UDP Server and Client

Rate me:
Please Sign up or sign in to vote.
4.97/5 (46 votes)
21 Dec 20059 min read 246K   8.2K   134  
An article that shows the implementation of a lightweight UDP server and client with optional reliable channel.
/*
 * Genesis Socket Server and Client
 * (C)Copyright 2005/2006 Robert Harwood <robharwood@runbox.com>
 * 
 * Please see included license.txt file for information on redistribution and usage.
 */
#region Using directives

using System;
using System.Text;

#endregion

namespace GenesisCore
{
	/// <summary>
	/// Enumeration containing filter flags for the broadcast methods.
	/// </summary>
	[Flags]
	public enum BroadcastFilter : int
	{
		None		= 0,					//Filter out everything
		Servers		= 1,					//Send to servers we are connected to.
		Clients		= 2,					//Send to clients connected to us.
		All			= Servers | Clients,	//Send to both servers and clients (every connection).
		AuthedOnly	= 4,					//Only send to authed clients or servers we are authed with.
	}

    public class GenesisConsts
    {
        //Command packet handling constants
        public static int MAX_EXCEPTIONS = 10;				//Maximum concurrent exceptions in receive loop before it shuts down
        public static int MAX_COMMAND_LEN = 512;			//Maimum command packet length (larger are split up)
        public static int MAX_FIELD_LEN = 2000;				//Maximum field length in a command
        public static int MAX_FIELDS = 50;					//Max number of fields allowed in a command
        public static int CONNECTION_TIMEOUT = 15;			//Seconds between pings before a connection is deemed timed out
        public static int CONNECTION_CLEAN_TIME = 3;		//Seconds between timed out connection cleanups
        public static int CONNECTION_PING_DELAY = 5;		//Seconds between pinging server connections
        public static int CONNECTION_RELIABLE_RETRY = 2;	//Seconds between resending reliable packets
		public static int CONNECTION_TIMEOUT_CHECK = 2;
		public static int CONNECTION_TIMEOUT_TIME = 5;
		public static int BAN_CHECK_TIME = 20;				//Seconds between checks of expired bans.
		public static int DEFAULT_MAX_WARNINGS = 3;			//Number of warnings an IP can get before being banned.
		public static int MAX_RELIABLE_QUEUED = 25;			//Maximum number of reliable commands that can be queued.

        //Used to ensure the encryption key was ok.
        public static string ENCRYPT_CHECK_STRING = "GC"; //Sent with every packet to validate the encryption key

        //Internal standard command opcodes
        public static string OPCODE_COMPOUNDPIECE = "00";
        public static string OPCODE_CONNECTIONREQUEST = "01";
        public static string OPCODE_CONNECTIONACK = "02";
        public static string OPCODE_LOGINDETAILS = "03";
        public static string OPCODE_LOGINACK = "04";
        public static string OPCODE_DISCONNECT = "05";
        public static string OPCODE_PING = "06";
        public static string OPCODE_RELIABLEACK = "07";

        //Command packet flags
        public static byte FLAGS_NONE = 0;
        public static byte FLAGS_CONNECTIONLESS = 1;
        public static byte FLAGS_ENCRYPTED = 2;
        public static byte FLAGS_COMPOUNDPIECE = 4;
        public static byte FLAGS_COMPOUNDEND = 8;
        public static byte FLAGS_RELIABLE = 16;
        public static byte FLAGS_SEQUENCED = 32;

        //Function return constants
        public static int UDP_OK = 0;
        public static int UDP_FAIL = 1000;
        public static int UDP_NOTFOUND = 1001;
        public static int UDP_ALREADYCONNECTED = 1002;
        public static int UDP_ALREADYINQUEUE = 1003;
        public static int UDP_UNRELIABLETOOLONG = 1004;
        public static int UDP_RELIABLENODESTINATION = 1005;
		public static int UDP_UNABLETORESOLVE = 1006;
		public static int UDP_RELIABLEQUEUEFULL = 1007;

        //UDP engine states
        public static int UDP_STATE_IDLE = 0;
        public static int UDP_STATE_LISTENING = 1;
        public static int UDP_STATE_CLOSING = 2;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Born in England, I have been programming since a very early age when my dad gave me prewritten programs to type in and run on a Sinclair ZX81 machine (seeing my name printed out on a TV screen was enough to keep me entertained!). I later did work using basic and STOS basic on the Atari ST and after that got my first PC and used Microsoft's QBasic. Later when I was about 13 I was in an airport and saw a trial copy of Visual Basic on a magazine, which I bought and it got me hooked on the Microsoft development tools.

Currently I am studying a software engineering degree and have been working with .NET since 1.0. I have just moved over to Visual Studio 2005/.NET 2.0 and am loving it! During my degree I have worked for a year at DuPont, where I ended up changing a lot of their old existing software over to .NET and improving it in the process! Since then I have been back and done some consulting work involving maintaining some of their older C++/MFC software.

While most of my current interestes involve .NET I am also confident in working with C++ in Win32, VB, Java, and have even done some development work on the Linux platform (although most of this involved ensuring that software I wrote in C++ was platform independent).

I have a strong passion for software technology, both higher level and more recently, systems level stuff (the dissertation I am doing for my degree is to implement a small compiler and virtual machine in C# for a Pascal-style language).

Comments and Discussions