[C#] UDP (Potential MMOG server)

Here is something I’ve decided to release to you folks. It’s a very simple sample on how to use the UDP protocol. This doesn’t use Raknet in anyway shape or form.

The goal for this is for an MMOG a team and I are creating. So far with packet sorting, we have managed to get 50 clients going with zero problems… Pretty impressive for our first attempt.

Basic WebDemo: Click Here

Anyways, here’s some code.

Basic server:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Server1
{
    public class UdpServer
    {  
        public static void Server()
        {
            string welcome= null;
            byte[] data;
            try
            {
                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 27888);
                UdpClient newsock = new UdpClient(ipep);

                Console.WriteLine("Waiting for a client...");

                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

                while (true)
                {
                    data = newsock.Receive(ref sender);

                    Console.WriteLine("Message received from {0}:", sender.ToString());
                    string test = Encoding.ASCII.GetString(data, 0, data.Length);
                    Console.WriteLine("Message: " + test);

                    if(test == "*getMessage")
                    {
                    welcome = "24.144.241.209,=ARC=TeamFortress1.1.2 Assault@75.125.239.226,Aliens/Marines RaptorElite.com@75.125.239.226,Chaos Survivor RaptorElite.com@93.89.199.200,CZESC UKA@64.62.211.150,Frosty  |RSC| - CMP@64.62.211.150,Frosty's Playhouse-No Track/SK@75.48.230.150,Fusion Nightmare's@0.0.0.0,GameArena Alien vs Predator 2 DM@0.0.0.0,GameArena Alien vs Predator 2 Overrun@0.0.0.0,GameArena Alien vs Predator 2 TDM@75.125.239.226,INSANITY RaptorElite.com@66.31.199.244,Join Already@125.202.114.216,LAST SAMURAI SERVER@125.202.114.216,LAST SAMURAI SERVER[RailGun Only] @74.208.65.174,LithFAQ 1.3 New Maps@75.125.239.226,Mayhem Pred/Corp RaptorElite.com@79.116.202.134,Night time CTF@79.116.202.134,Night Time Domination@0.0.0.0,NOT v1.3: Hunt | [url]www.lithfaq.com@0.0.0.0,NOT[/url] v1.3: [-Hell Hounds-]http://www.thehellhounds.org@94.142.233.88,Rommie's BETA GameParty.cz TDM@77.207.22.62,Server JV.com (Le Celticant)@208.122.52.162,SG-2 EVAC Rommie 5.0 Beta@208.122.52.162,SG-2 Life Cycle Rommie 5.0 Beta@208.122.52.162,SG-2 Marines vs Aliens Vs Preds Rommie 5.0 Beta@98.228.151.8,The G|2avel Pit | [Chicago]@89.167.22.211,UMP2 PARTY![url]www.umpparty.pdg.pl@72.51.60.213[/url],[Alpha] Evac (US)! -www.lithfaq.com Master Server Mod@89.222.154.57,[RUSSIAN BEARS] Capture The Flag - 1.1.2@89.222.154.57,[RUSSIAN BEARS] Duel@89.222.154.57,[RUSSIAN BEARS] From Russia With Love | AVP.BETSOL.RU@89.222.154.57,[RUSSIAN BEARS] Predators Hunt Marines - AllWeapons@89.222.154.57,[RUSSIAN BEARS] Survival Of Predators - NO NOOB Weapons@89.222.154.57,[RUSSIAN BEARS] UMP2 Maps v1,2,3,4@212.14.30.82,[SKY-3]-PL (TDM - HvsA) [[url]www.sky.prv.pl]@212.14.30.82[/url],[SKY]-PL [[url]www.sky.prv.pl]@62.249.244.207[/url],[TRIBE]  | Helde Van Afrika | - Lithfaq 1.3@93.179.215.248,[U]ltimat[E]_PL@62.249.244.207,{CA} - Aliens Vs Corps - Lithfaq 1.3@62.249.244.207,{CA} - Marines Vs Preds - Lithfaq 1.3@64.62.211.150,|RSC| Raven's Nest@71.53.188.71,|RSC| The Asylum";
                    }

                    data = Encoding.ASCII.GetBytes(welcome);
                    newsock.Send(data, data.Length, sender);
                    sender = null;
                    //GC.Collect();
                    Console.WriteLine("Message Sent");


                }
            }
            catch
            {
                Console.WriteLine("Error - Could not bind port");
            }
        }
    }
}

Unity package: Click here.

:slight_smile:
should help people getting an idea what to search for through google when they look for docs and tutorials :slight_smile:

As for the code itself: I would recommend to not send all those servers in one go but instead 1 server per packet. Unsure if System.NET handles that internally but if not, you could run into the problem that your packet gets too large and won’t be received correctly anymore.

Your quite correct. If there is too much data it will simply be cut (99% sure anyways). But this is just a simple simple test so its moot.

Oh one more thing, this doesn’t include any NAT traversal so you will have to open port 6050 to test this.

i cann’t visit :cry: