UDP Multicasting Clients and a Name Server


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 clients and a shared object to maintain client information. Each client employs a thread to watch for server communications (typically copies of other users' messages).


UDP multicasting clients and a name server

The clients send messages to each other via UDP multicasting. However, a client wishing to join the chat group must first communicate with a name server, which sends it the group address or rejects the request. A departing client must notify the name server, so it can maintain a current list of participants. A client wanting a list of other users sends a message to the server, rather than querying the group.


Clients using a servlet as a server

The clients communicate with a chat servlet, sending messages as arguments of the servlet's URL. The servlet maintains client information, which is shared between the multiple threads executing inside the servlet instance. Each client has a separate thread which periodically queries the servlet for any new messages.

These examples can be found in the Chat/ subdirectory. The client/server example is in Threaded/, the UDP multicasting version is in Multicasting/, and the HTTP tunneling code is in ChatServlet/.




Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net