O'Reilly logo

Killer Game Programming in Java by Andrew Davison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 30. Network Chat

In the last chapter, I presented an overview of several types of network programming, including client/server, peer-to-peer, and HTTP tunneling. This chapter revisits those topics to see how they can be employed to build networked chat applications. Chat capabilities are found in almost every multiplayer game (e.g., in shared virtual worlds).

The main characteristic of a chat system is its dynamic nature: clients can join or leave at any time, and there's no fixed order to when people can speak. A message sent out by a user should be delivered to all the other clients, preferably labeled with that user's name or ID.

The chat space may be divided into distinct regions or groups, with varying levels of access and communication privacy. The system may allow personal communication between two users, unseen by other people. I'll discuss ways of implementing private messages in the examples.

In this chapter, I'll code the same chat system using three different approaches: client/server, UDP multicasting, and HTTP tunneling to a servlet. Chat messages are broadcast, and a special who message returns a list of the other clients. A client leaves the system by sending a bye message. The multicasting and servlet versions of the application introduce a hi message for joining and support a simple form of private one-to-one communication.

Here are the key technical details of the three systems:

Threaded TCP clients and server

The server uses threads to communicate with its ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required