Thursday, July 24, 2008

Multiplayer Game Basics

Multiplayer games come in all kinds of formats, genres, and languages. In this article, I will discuss the bare-bone concepts of a multiplayer game and even give step-by-step intructions for making a multiplayer backend (server) with Java. While I will only be discussing Java specifically, all the concepts apply to all programming languages. The general concepts require no real prior programming knowledge, though the Java tutorial accompanying this article requires a basic understanding of how Java works.

Multiplayer games can be put into one of two basic structures: Client-to-Client or Client-to-Server. In Client-to-Client games, the client communicates directly with another client, while in Client-to-Server, clients communicate with a server without ever having to talk directly to the other clients. This article will discuss Client-to-Server communications as this is usually the more preferred type for multiple reasons, which are outlined in the following table.

Advantages of
Client-to-Client vs. Client-to-Server
Client-to-Client

  • Low to no cost of operation to publisher
  • No reliance on publisher
  • Individual modifications
Client-to-Server

  • Security of clients
  • Slower clients dont slow down others
  • Centralized statistics
  • More quickly updated
  • Clients experience the game the way it was meant to be


All of these advantages and their inherited disadvantages can be traced to the simple fact that Client-to-Client games are individually owned and maintained by those who play them, where the Client-to-Server games are based around a "centralized" system of computers that are all maintained by the game publisher or others who are given permission to do so by the publisher. Before we begin the in-depth portion of the article, I want to clarify what a server is. A server in multiplayer games refers to a variant of the game or a related program that allows clients to communicate with it, and subsequently relay those communications to other clients who are connected to the same server. This web of client connections is called a network. Ironically, even in Client-to-Client structures a server exists. The server in this structure is more commonly referred to as the host, since their computer is not dedicated to serving the network.

Connections between computers over networks (i.e. the Internet or LAN) is usually accomplished with a system of sockets. A socket is a pipeline linking the server and client through a computer port. Each client shares a unique socket with the server, so no other clients can read data meant for other clients. Data traveling between computers is referred to as a packet. Most programming languages support the use of sockets and packets. The servers job is to listen to a socket, take in packets, do what needs to be done with it, and possibly reply to the client with another packet, or even broadcast packets to other clients currently connected.

Now that we understand the technology involved, its time to get down and dirty with Java. We will be creating a simple Java application that listens for incoming connections, designates the connection to a socket, and replies to each packet sent by the client after that. Programming in Java requires a text editor (I use good ole Notepad), the Java Runtime Environment (JRE), and Java Development Kit (JDK). Both the JRE and JDK can be downloaded from Sun's website. Java source code is saved with the extension .java. After compiling, the resulting programs are saved with the extension .class.

I have already programmed and tested the Java applications. You can download MplayServer.java and MplayClient.java from SNR, along with a simple client named MyTalker.java. To compile and run the server, navigate to the directory you saved the source files to with command prompt, then use the following commands:

javac MplayServer.java
java MplayServer 8087


To compile and run the simple client, use the same commands as above, except replace MplayServer with MyTalker.

Edit: The full step-by-step tutorial is done. You can find it at http://www.subnetroot.com/source/j/Basic/tutorial.txt.

8 comments:

Anonymous said...

Very nice Blake ! keep up the good work, m following you blog from today itself!

Unknown said...

Thanks for this, although I can't access the files its a good starter post. Will your server ever be back online?

Blake said...

Sorry, Binxalot. The old Blakenet server will most likely not return. However, I will migrate all data provided by it to the new Subnetroot server at some point.

In the meantime, you can find this articles resources at http://www.subnetroot.com/source/j/Basic/ . Sorry for the inconvenience, and thanks for reading!

Anonymous said...

Interesting. However the link does not work : http://blakenet.no-ip.org/source/j/Basic/tutorial.txt

Can you do something about it ?

Blake said...

Absolutely. You can find the resources at http://www.subnetroot.com/source/j/Basic/ now. I just forgot to change the links after the server move.

Thanks for reading!

jo said...

Getting error at client.start() line...... please help

Blake said...

What is the error you are receiving? (use e.printStackTrace() from the catch block at MplayServer.java line 53)

Unknown said...

This is such an interesting blog. You are very knowledgeable about this subject. Please check out my site.
mafia siciliana