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.
Client-to-Client vs. Client-to-Server | |
Client-to-Client
|
Client-to-Server
|
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.