Netgame Hub and Connection Requests
The chat room application consists of two programs: a server, ChatRoomServer.java, and clients.
Counting the Programs Behind a Chat Room
A chat room can look like a single thing on screen, but its architecture is built from separate pieces that cooperate. Before reading any code for a multiplayer or messaging program, it helps to know how many separate programs make it up and what each one is responsible for.
Try this question before reading on: if two people are chatting, does the chat room consist of one program running on both computers, or does it consist of more than one program with different jobs? Keep your answer in mind as you read the next sections, and check it against what the source describes.
Tracing the Server's Startup and First Request
Follow the order of events. The server starts by creating its netgame Hub, and that Hub listens for connection requests. A client then sends a connection request, using the same port the server is listening on. The diagram below shows these messages in order.
The diagram stops at the connection request because the source describes the steps only up to that point. It does not say what happens to messages after a connection is made, so this article does not describe that part of the program.
Two Programs with Separate Jobs
The chat room application consists of two programs: a server, found in the file ChatRoomServer.java, and clients. The server is the first program. The source describes it as a completely trivial program that does very little on its own. Its job is to create a netgame Hub that listens for connection requests from netgame clients.
The second part of the application is the set of clients. Their job is to send connection requests to the Hub. The division is simple: the server's responsibility is to be present and listening, and the client's responsibility is to ask to connect.
The Netgame Hub at the Center
The central mechanism on the server side is the netgame Hub. The server creates a Hub so that it can listen for connection requests from netgame clients. The Hub is the point where clients knock to ask for a connection.
Where the Port Number Fits
A port number is the value that lets the server and clients agree on where to meet. In this program, the port number is named PORT and is defined as a constant. A constant is a named value that is set once and is not meant to change. Naming the port this way means the value lives in one place and can be referred to by name.
The source is clear that the number itself is arbitrary: any value can be chosen. The one requirement is that the server and all clients use the same value. The specific number has no special meaning in this program. What matters is that both sides agree on it.
Tracing one connection request with a chosen port
Suppose a developer chooses some valid number as the value of PORT. Trace how the server and a client use that value to connect. This example is generated for teaching and uses a made-up number, not a value from the source.
- 1Choose the shared value
The developer sets PORT to one chosen valid number. Because the source says any value works, the choice itself is not the important part.
- 2Server listens on that value
The server creates its netgame Hub, which listens for connection requests on the port named PORT.
- 3Client uses the same value
A netgame client sends its connection request using the same PORT value that the server is listening on.
- 4Check the requirement
Both sides use one shared value, so the stated requirement is satisfied and the request reaches the Hub that is waiting for it.
Any valid number works, as long as the server and every client are set to that same number.
Why Mismatched Ports Break the Setup
| Situation | Port values | Status of the requirement |
|---|---|---|
| Matched setup | Server and all clients use the same PORT value | Satisfied |
| Mismatched setup | A client uses a different value from the server | Broken |
The only requirement on the port is that every side uses the same value. A mismatch violates that requirement.
A mismatched port number breaks the stated requirement, so the fix is to align both sides on one shared value. Because the number is arbitrary, you never need to change the number to make a mismatch work. You only need to make the server and clients agree.
Common mistake Believing the port number has a special meaning that must be a particular value
Why it failsThe source says the value is arbitrary and any value can be chosen.
Do this insteadPick any valid value, then use that same value on the server and on every client.
Common mistake Changing the value on only one side
Why it failsThe server and all clients must use the same port. A mismatch breaks that requirement.
Do this insteadChange PORT in a way that keeps the server and every client aligned on one value.
Common mistake Assuming the client itself creates the Hub
Why it failsThe server creates the netgame Hub. Clients send connection requests to that Hub.
Do this insteadKeep the roles separate: the server creates and listens, and the clients ask to connect.
Checking Your Understanding of the Two Roles
A teammate says that the chat room needs a Hub subclass that they wrote themselves, and that the client programs should create the Hub so that they can listen for requests. Using what you have read, explain what is wrong with each part of this claim and state how the server and clients actually divide the work.
Hints
- Look at what the source says about whether the chat room uses the Hub class itself or a subclass of it.
- Recall which program creates the netgame Hub and which program sends connection requests to it.
Explain in your own words why two programs using different port values cannot satisfy the requirement, even though the number itself was chosen arbitrarily.
Hints
- Focus on the requirement the source states, not on which number is better.
Key Takeaways
- The chat room application consists of two programs: the server,
ChatRoomServer.java, and the clients. - The server creates a netgame Hub using the standard Hub class, and that Hub listens for connection requests from netgame clients.
- Clients send connection requests to the Hub using the same port the server listens on.
- The port number is named PORT and is an arbitrary constant value, but the server and all clients must use the same value.