# Is C# viable for a real-time game server? [closed]

Most of the question is in the title.

Basically, I'm starting to develop a 2D multiplayer action game. The client is (probably) going to be in XNA, and I thought I'd ask here about whether it's a good idea to use C# for server side development as well.

I guess the fact that the language is managed is not something problematic as Java is being used in even MMO game servers (correct me if I'm wrong), so are there any reasons to avoid C# for this purpose? I need it to be fast and responsive, to communicate over TCP, to chat with an SQL server. And if there is no reason to avoid C#, how would I deploy such a web server, would it be a .NET EXE running on the server machine?

• You are correct about the Java MMO server. – Ricket Mar 24 '11 at 17:58

Not only viable, but really good, in my experience. I've developed several MMO servers using C#, and I must say I never regretted the choice of language and platform.

There are lots of great libraries and tools for C# and .NET in general - network, logging, O/R mapping, etc. And, compared to Java C# is more expressive and less verbose (some people might argue about this, though..)

The GC "overhead" that scares some people is not really an issue, unless you happen to abuse it with billions of allocations per second. As an example, our current server allocates up to 50 mb/second under heavy load, and GC does not introduce any noticeable lag. We did have to use object pooling in strategic places, though - most importantly, objects representing network packets are pooled and not garbage-collected. Still, even with pooling turned off, GC is not the biggest problem.

As an example of how cool C# is, this is what we recently implemented. Our server runs several WCF services, that the game client uses for non-time-critical tasks, and we also use them for server administration. It turns out, it's very easy to make a WCF service to simply return our game objects to the caller. So we did just that, then made a little plugin to LINQPad that connects to our server - and now we can run queries like

from character in Service.GetOnlineCharacters()
where character.LocationManager.LocationId==5 && character.Attributes.Level<10
select new { character.Id, character.Nick }


On a live server, no less! I don't think you can do this with any other platform. Not in couple day's work, at least.

• I especially like the use of LINQ, this makes the server so much simpler and faster to develop. – Thomas May 27 '13 at 11:13

Sure, you can use C#.

One of the bigger issues to be aware of in terms of performance in C# or other .NET ecosystems is the GC -- the .NET framework provides several flavors of GC, including a "server" GC, that are worth learning about. Intelligently managing memory (for example, by pooling) is still a good technique even in a managed environment.

There are several robust APIs for interacting with SQL, communicating via TCP, et cetera in C#, either as part of the base framework or via third-parties, so you should have no trouble there.

You can utilize ASP.NET or similar if you really want your server to be web-based; if you just want to run a process and listen for TCP connections you can just deploy an .exe and the appropriate dependencies to the server machine, open up the appropriate ports, and run the .exe.

I think it's a great idea. The language is certainly capable of it, and especially if it's what you know, don't spend a bunch of time learning a new language just because it's "faster". The real bottleneck of your server will be network latency; an extra millisecond or two because you used C# instead of C++ won't make a difference.

• The real risk of game development (or any real time development) in managed languages is not that everything takes a millisecond or two longer, but that random frames will take tens of milliseconds longer because the GC decided to finalize things. Encouraging throughput-focused comparisons with terms like "bottleneck" is misleading. – user744 Mar 24 '11 at 23:40

If it's okay for you to deploy on Windows, I'd strongly recommend it, especially since your client is C#.

Don't underestimate game server development though. Even making a simple chat-only concurrent server is no trivial task, and you'll soon have lots of questions regarding concurrency, locking, performance, etc.

Just as a starting point, I'd like you to take a look at this page that broadly explains how to do concurrent socket programming in several platforms:

http://geekswithblogs.net/quension/archive/2006/06/30/83760.aspx

If you're really serious about it, I'd recommend you thoroughly study Stevens' Unix Network Programming. It's focused on C sockets on Unix, but the principles are the same for every other platform, including .net.

Edit: This is another great resource, focused on scalability and performance for concurrent servers in .net. It's no longer available, but our friends at the wayback machine have a copy of it.

http://replay.waybackmachine.org/20080405220143/http://www.coversant.net/dotnetnuke/Coversant/Blogs/tabid/88/EntryID/10/Default.aspx

UPDATE (2020): The following paragraph is no longer true due to dotnet core/dotnet 5.

One 'disadvantage' would be that with C# if you want to use it on multiple platforms you have to be very careful to make sure that your code will work with mono. If all you care about is running it on a single platform and windows is that platform, then you should have no problems as others have mentioned.

• Don't forget that MONO can be used to deploy on other platforms. I believe Second Life uses this approach - their internal game scripts are actually compiled to .NET IL yet they deploy on Linux AFAIK. – ShadowChaser Jun 29 '11 at 19:50
• Update: Now C# works on cross platform using dotnet core runtime. – TheMisir Apr 10 '20 at 12:41