Popular languages for networking for games

Started by
7 comments, last by menyo 8 years, 7 months ago

I am extremely surprised that every time I tried searching for "game networking" I literally see no useful results at all. It's like a topic that none ever talks about.

I want to know what are the most commonly used languages as far as networking a game goes. I am a intermediate/advanced programmer with C# and Java experience.

I have concluded that Javascript and Java are both popular for game networking. Is this true ?

If I am using Java with LibGDX would you say that using Java for the server-side is more appropriate than using Javascript ? Is there a difference in performance really ?

Opinions ?

Advertisement
Java and Javascript are about as similar as C# and PHP, or C and Lua. Their names are similar but they are in no way related.

A game's network code is typically written in the same language as the rest of the game engine. If you're writing a game in C++, your networking code will likely be C++. Same for Java, or C#, or Javascript.

If you're writing your game in Java, the easiest approach will be to just stick with Java.

I know that Java and Javascript are completely unrelated. I didn't made clear that I knew that, but I thought it was to be assumed, since I said I am not a beginner in programming by any means.

It is interesting that you say that "the network code is typically written in the same language as the game engine" because I had exactly the opposite idea about it.

The reason I mention Java is because my games are using Java right now. And the reason I mentioned Javascript is because I got a feeling that Javascript is popular for the networking code and much (?) more efficient than Java or C# for instance.

So I was trying to compare Java, with a more popular language for networking, like Javascript. Do you think my assumptions for Javascript popularity and efficiency for the backend are true overall ?

My other games were using C# with XNA and when I tried to write some networking code in C# and actually run it on my server, I had horrible experiences running it on linux and I also heard that it is inefficient. So I am wondering what is the efficient / popular way of writing code for the server-side, specifically for a game's networking code. I would much rather start with a popular one, to get experience with something that is commonly used professionally since I am mainly a programmer and a hobbyist game developer. So I don't want to invest time in something that is not commonly used in the programming field.

No, Javascript is not "a more popular language for game networking". It's a more popular language for webpages.

I think perhaps we're using different terminology than you are, and because of the terminology confusion, you might be getting confusing results from your web searches.

When someone says "game networking", usually that refers to a game Client that talks to a game Server (like how World of Warcraft, League of Legends, or Modern Warfare's multiplayer works). Internet "packets" (blocks of data, similar to files saved to a harddrive) get sent between the Client and the Server to communicate information between them.

Whatever language the game client is written in, the same language is used to send and receive packets on the client's side.

Whatever language the game's server is written in, the same language is used to send and receive the packets on the server's side.

The server (and the server-side networking) could use pretty much any language. Javascript would work, but is not the most popular choice, and in many situations would actually be a bad choice (depending on the nature of the game).

As for the packets themselves, they could be described using a Domain Specific Language to generate the code for the packets, but they don't have to be and often aren't. Sometimes the packets are written out as JSON or XML or another markup language. They don't have to be and often aren't.

Javascript, while used for many purposes, is most well known for running web pages in a web browser and occasionally doing server-side processing as well. This is an entirely different category of applications than game networking.

What language are you making your game's client in? Java? Then use Java for the client-side networking.

What language are you making your game's server in? Java would work - I'm not a huge Java fan, but it would work. Javascript wouldn't be much better.

Servers require alot of performance, depending on the nature of the game and the number of people connected at once and the hardware it's running on.

C++ seems like the best choice for that, but I'm biased in C++'s favor.

If I were in your position:

If your game is a larger project, I'd look into using C# (running on MONO - you'll want the server to be able to run on Linux) to write the server since you already know C#. I think C# would be better suited for a server than Java, but that's a hunch on my part - I'm not aware of any C# MMO servers.

If your game is a smaller project, then I'd just write it in Java, because one major benefit of writing the server in Java is sharing code between the client and server.

Disclaimer: I'm speaking from accumulated knowledge rather than actual experience - I've read alot about these subjects, but haven't actually written a networked game before.

Thanks a lot for your very informative answer.

You seem to favor C# over Java for networking, however, I have heard that Mono is about 50% less efficient than the original .NET framework, which is required for a Linux server and that makes me think... I have also heard that Minecraft, was done purely in Java, including the networking code, so how bad could it be.

As you said, because game networking is not the same thing as a small server-side process running for the purposes of a website, that makes Javascript not really suitable for networking code for games. So overall, you think that mostly C++ is the way to go ? And then C# and Java are just on the side ?

I have never really had the interest or time to get into web development, but I thought that the languages that are popular for the web, like Javascript/Python/Php/Ruby etc. , would be popular in game networking as well.

I have also heard that Minecraft, was done purely in Java, including the networking code, so how bad could it be.


Excellent question! How bad can Minecraft be? Let me count the ways...
A) It's incredibly laggy.
B) It's an abominable RAM hog.
C) It can't support a large number of users without really good hardware.

Minecraft was an amazing runaway success, financially-speaking. I also enjoy it as a player, and have spent alot of time playing it.

Just because I like it, or just because it made alot of money, that doesn't mean it was engineered well.

As you said, because game networking is not the same thing as a small server-side process running for the purposes of a website, that makes Javascript not really suitable for networking code for games. So overall, you think that mostly C++ is the way to go ?

Only if you already know C++. Unless you need to, I wouldn't bother learning a whole new language just to write your server.

C++ ultimately is the most efficient language, if you can skillfully write your C++ code. The downside is that it's also the easiest language to shoot yourself in the foot with, the hardest language to learn, and the slowest language to code in. Many projects don't need to be written in C++.

This is why I was suggesting C# or Java - you already know them. Depending on your project's individual needs, I'd either code the server in Java so the client and server are both using the same language, for development convenience. Or I'd code the server in C#, because my intuition is that it'd be the better choice for a larger project trying to host hundreds of users.

Supposedly, it's possible to write an efficient program in Java - but I've never seen one.

I wonder what RuneScape's server was written in - I can't find any hard facts (only internet guesses), but it might be Java, and if so, seems to do its job well enough.

I have never really had the interest or time to get into web development, but I thought that the languages that are popular for the web, like Javascript/Python/Php/Ruby etc. , would be popular in game networking as well.

Nope. For major games, C++ is the most commonly used (for performance and portability reasons - most consoles over the years pretty much required C++); since C++ is already being used for the game itself, there's no point switching languages just to write the networking part of that game.

Popularity also does not equal "good". PHP is very popular (almost ubiquitous when it comes to server-side web pages), but many people feel it is a badly designed language.

Opinions vary widely.

When people talk about "game networking", there's a MASSIVE amount of work within the game client itself to deal with multiplayer.
The details of the multiplayer replication strategy usually end up permeating all of the gameplay code and large chunks of the engine.
So when you were asking about "game networking" that's why I was a bit confused -- it's intertwined with your engine and gameplay code, so of course will be the same language.

When it comes to choosing a language for writing a dedicated server for a game, that's a different question :)

Lots of games don't even have servers (in the web sense - a linux machine in a data center somewhere on the internet). In many games, the game client is capable of acting as a game host, either in a peer-to-peer topology, or in a traditional client-server star topology with one of the clients acting as the server. In those games, whatever langiage used by the client will almost certainly be the language used by the server.

In games that actually do have dedicated servers in the cloud, there is often still a lot of shared code between the client and the server.
e.g. both client and server may need to perform collision tests against the world's geometry. You don't want to write that logic twice, so it makes sense to pick one language and write that logic in a shared library/module.


For example, the quake code architecture still seems popular (used by Valve, Crytech, etc) -- you write your game as two modules/DLLs, which communicate via the game engine's "network" system. One contains client-only details - rendering, HUDs, menus, audio playback, user input, etc... And the other contains all of the game rules, entities, AI, etc..
In single-player mode, the engine loads both modules and creates a "dummy" network system, which just copies packets immediately between the two.
In player-hosted multiplayer, one user loads both modules, while everyone else just loads the client module.
For dedicated servers, you port just the server module to Linux and run it in your data center. Players just load the client module and connect to your server.
The two modules could use different languages, but it's easiest to pick one that works for both.


I have seen Node.js (javascript) gaining popularity, but usually only for web-ish and metagame services: account management, password resets, chat servers, friend lists, matchmaking, etc. I haven't personally seen it used to write a full dedicated game server for a traditional C++/Java/etc game client.

This is a great site that summarizes things you need to consider when adding network support to your games: http://gafferongames.com/networking-for-game-programmers/

I am no expert on the subject but I do not think there is a "most popular" language for networking. As long as you have access to sockets you will be fine. As with everything, the more control you have the better you can create something that fits and works best with what you are planning. Therefor I guess C++ would be the language of choice. However if you are here to make games rather then building a framework or engine keep reading.

You mentioned Java and LibGDX and I'm a great fan of LibGDX. Nathan, the top contributor to LibGDX also made a Networking framework called KryoNet designed for Java. KryoNet works perfectly with LibGDX Android, Desktop and HTML modules, I'm not sure about OS. You can setup a client to server connection in a matter of minutes.

Put this in your core dependencies of "build.gradle" and you are ready to go.


        compile "com.esotericsoftware:kryo:3.0.1"
        compile ("com.esotericsoftware:kryonet:2.22.0-RC1") {
            exclude module : 'kryo'
        }

I currently have a server running on a Raspberry Pi using a regular Java Application. You basically only need this as the server to connect a client.


    public static void main(String[] args) throws IOException
    {
        final Server server = new Server();
        server.start();
        server.bind(tcpPort, udpPort);
    }

Now for the client which could be your LibGDX game.


    private void connectToServer()
    {
        Client client = new Client();
        client.start();
        try {
            client.connect(5000, "mydomain.com or localhost/127.0.0.1", tcpPort, udpPort);
        } catch (IOException e) {
            Gdx.app.log("Main", "Failed connecting to server.");
        }
        if (client.isConnected())
        {
            Gdx.app.log("Main", "Connected to server.");
        }
    }

Run both and you will connect. If you run the server from your desktop and want connect from outside instead of local(host) you need something like noip.com which is a free service that links your dynamic IP to a DNS. Of course you need to forward your ports to make this work over internet. Otherwise you could pay for hosting the server elsewhere.

To sent data back and forth you need to register so called "packets" in both client and server. A packet could look like this:


public class MessagePacket {
String name;
String message;
}
Add and register that class in both client and server:

server.getKryo().register(MessagePacket.class); //in server program
client.getKryo().register(MessagePacket.class); //in client program
Now we can sent stuff back and forth between server and connected clients. Let's send a message from the client to the server:

MessagePacket message = new MessagePacket();
message.name = "Menyo"
message.message = "Hello everyone!"
client.sendTCP(message);
And off it goes to the server. Now let's intercept it at the server.

    server.addListener(new Listener() {
        @Override
        public void received (Connection connection, Object object) {
            if (object instanceof MessagetPacket) {
                 //cast object to correct packet.
                 MessagePacket message = (MessagePacket) object;

                 System.out.println("Received message from client: " + message.name + " with connection ID: " + connection.getID);

                 //Check the message for bad words if you want here :)

                 //Sent it back to other connected clients.
                 server.sendToAllExceptTCP(connection.getID(), message);

            }
        }
    }

The server has received the message and sends it to all other clients. The last step is to listen for the Message on the client side and show it to the user. To receive a message at the client side you do the same as the previous step. Just listen for "received", cast it to the proper class and do you magic on it.

Since you now know how to sent and receive data on both server and client side nothing can stop you to make some amazing networking games! Well that's only partly true, creating a client/server structure requires different thinking and things can get more complicated faster then a offline game. But luckily, practice makes perfect.

This topic is closed to new replies.

Advertisement