XNA UK User Group
A helping hand for bedroom coders throughout the land.
XNA Socket/UDP Server

Blogs

RandomChaos

Syndication

 

The other day I was discussing with a friend (Jeremy) MMOG's and how we might go about writing one, we then went on to chat about MUD's we had written in the past and so lead me to the idea of creating a socket server; as I had had some experience of writing sockets in C++ and VB I thought I would give it a go in C#. Turns out there are a tone of articles on line that you can reference for this sort of stuff. Having written most of the socket side another friend of mine (Chr0n1x) suggested looking at writing a UDP server as that's what is mostly used in games development these days.

If you are not up on Sockets and UDP, take a look here for a pros and cons.

I guess your first question will be, what is wrong with using the Live networking that comes with XNA 2.0, my answer is, nothing, by all means use it, but if you don't want to go through the Live network, then you will have to write your own server and client system. This is a way to start doing that. This post only has the basic connection management, you will naturally have to add the messaging system that your game will need but you can also see a rudimentary example of this in the way the server accepts clients and adds them to the client list.

Your next question will by, why put it in an XNA project, why not, means I don't have to write the message pump and just create it as a Game Component and write the rest around it. Also, you could then have the server in the game code as well and then have one game serving with the others as clients on a LAN or even if you have a static IP, serve it on line (may be some security issues there :P)

Well here is how I have laied it out in this example.

As you can see in the class diagram above, there four classes, two enumerated types and six delegates.

Classes:

BaseServer has all the basic items both server types will need; events to inform the user that connections are being attempted, dropped and that data has been received etc.. also basic fields and related properties that both servers will use like MachineName and Port.

SocketServer is the class used to create a socket server (well named or what eh?). It has a few extra fields and properties for the serving IP and a TcpListener to obtain connections to it. It also overrides some of BaseServer methods to get extra things done in those methods.

UDPServer is the class (you guessed it) used to create a UDP server. As with the SocketServer it has extra fields and properties with overrides to BaseServer to do the jobs it needs to to as a UDP server.

ClientData is used to store your client/player data, it has basic authentication for adding a server generated Guid to uniquely identify each user. In this example I have not added any other data objects than are needed to bind clients to the server.

Enumerators:

The enumerators are my stab at a very basic messaging protocol, you can use this, extend it or even throw it out and create your own.

MessageType is the main message type so I can split out messages that are to passed strait on to another user like an in game message or if it is data that is going to change an item or items in the world it's self etc..

MessageSubType, so you have a min message type and these can then be split down further, so staying with a player to player message, one message could be to all on players like an OOC (Out Of Character) message or could be a personal message sent to a single player in game.

Delegates:

The delegates define the event signatures that a user of the classes can use to wire up events from the server.

ConnectionAttemptEvent is fired each time a client/player tries to connect to the server.

ConnectionDroppedEvent is fired when a clients connection is dropped or lost.

DataReceivedEvent is fired when data comes into the server from a client.

ErrorEvent is fired when an error occurs on the server.

ServerStartEvent is fired when the server starts listening for clients.

ServerStopEvent is fired when the server is stopped.

In the example project you can download there is also a test client, all that this client does is connect to the service and can send text messages to it, nothing else, it's pretty rough realy. The server example using the objects described above just allows and acknowledges connections to it and drops them if they are inactive for the default ClientTimeOut, which I set at an arbitrary 30 seconds.

You can get the example code here.


Posted Mon, Jan 21 2008 10:13 AM by Nemo Krad

Comments

GameDevKicks.com wrote XNA Socket/UDP Server
on Mon, Jan 21 2008 2:46 PM

You've been kicked (a good thing) - Trackback from GameDevKicks.com

leaf wrote re: XNA Socket/UDP Server
on Mon, Jan 21 2008 3:03 PM

Not to prevent you from writing your own but your readers might be interested in this .Net UDP networking library, which looks to be a very good choice for those not using the XNA Live stuff.

code.google.com/.../lidgren-library-network

Nemo Krad wrote re: XNA Socket/UDP Server
on Mon, Jan 21 2008 3:19 PM

Cool, that looks like a nice library.

Going to post my code anyway as it has both Socket and UDP servers in it, not as evolved as that lib though....

Jeremy wrote re: XNA Socket/UDP Server
on Tue, Jan 22 2008 11:26 PM

:) I like the code but I hope when we finish this production code it will be a bit more robust. You write code faster than I can read it nemo I need to quit thinking about design and type more...

Nemo Krad wrote re: XNA Socket/UDP Server
on Wed, Jan 23 2008 9:32 AM

LOL, writing code fast is not always a good thing, design should always come first. I tend to act then think about the code later...

owen wrote re: XNA Socket/UDP Server
on Sat, Oct 4 2008 5:05 PM

I load the Game Server... But where is the Client for it?

Nemo Krad wrote re: XNA Socket/UDP Server
on Sun, Oct 5 2008 8:58 PM

There are three zips in that download. I believe the client is in the TestSocketClient.zip file.

Sci wrote re: XNA Socket/UDP Server
on Mon, Jan 18 2010 4:22 PM

Hi i ran your server and client, server works fine however the client did not run so smoothly, i'm getting an error.

"No such host is known", when I // comment out ln 89 it works but does not connect to the server then. can you explain why i'm getting such errors? am I doing something drastically wrong?

Nemo Krad wrote re: XNA Socket/UDP Server
on Fri, Jan 29 2010 12:13 PM

Are you running the samples or your own version (copy/paste) of them??

If the latter , send me your code ad I will have a look.

Sorry for the late reply, it's been a very bust year so far...

abc123 wrote re: XNA Socket/UDP Server
on Wed, Mar 31 2010 5:11 PM

Running the sample files the same thing happens to me that Sci experienced. I'm not 100% sure if i'm meant to change "IP" and "Port" but it didn't work either way. I've forwarded ports, and tried every combination of ports and IPs including my IP address and a 127 local loop back. Any suggestions?

Nemo Krad wrote re: XNA Socket/UDP Server
on Thu, Apr 1 2010 3:05 PM

Once GS4 is released I will be looking into bringing my posts up to date.

Sorry for the late reply.