Constants and Shared Values in Programs
The chat room application consists of two programs: a server, ChatRoomServer.java, and clients.
Counting the Programs Before Reading Code
A multiplayer or messaging program can look like a single thing on screen. A chat room, for example, seems to be one window where messages appear. Before you read any code for a program like this, it helps to ask two questions: how many separate programs make it up, and what is each one responsible for? The chat room application is built from distinct pieces that cooperate, and this article examines that arrangement.
What do you think happens?
Given that the chat room is one application, how many separate programs do you think it needs in order to let people talk to each other? Make a guess before reading on.
Reveal answer
Two programs: a server and clients
The chat room application consists of two programs: a server, ChatRoomServer.java, and clients. The server's job is to listen, and the clients' job is to ask to connect.
The Server and the Clients
The first program is the server, found in the file ChatRoomServer.java. The source describes it as a completely trivial program. It 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, which send connection requests to that Hub.
The division is worth keeping in mind as you read the rest of the chapter. The server's responsibility is to be present and listening, and the client's responsibility is to ask to connect. The source does not go into further detail about what happens to messages after a connection is made, so this article does not describe that part of the program.
How the Server Listens Through a Hub
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 source makes a precise point here: the chat room uses the Hub class itself, not a subclass of it. The program does not define its own specialized kind of Hub. It works with the standard Hub class directly.
The server creates a netgame Hub, which uses the Hub class itself, and that Hub listens for connection requests from netgame clients. The server does not create a subclass of Hub for this job.
Tracing a Connection Request
Putting the pieces together gives an ordered sequence. The server starts by creating its 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 source describes the steps only up to the connection request, so the diagram stops there.
Following one connection attempt
A server has been started, and a client wants to join the chat room. Trace the steps the source describes, from the server starting to the client's request.
- 1Server creates the Hub
The server program creates a netgame Hub. The Hub is the standard Hub class, not a subclass.
- 2Hub listens
The Hub is now waiting for connection requests from netgame clients.
- 3Client sends a request
A client sends a connection request. For the request to reach the server, it must use the same port number that the server is listening on.
- 4Stop at the request
The source describes these steps only up to the connection request. What happens to messages after that is not covered here.
The server's Hub is listening, and a client's connection request arrives on the agreed port. The source does not describe what follows.
The Shared PORT Constant
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 then referred to by name. The source says 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 is arbitrary. It carries no special meaning of its own. What matters is that the server and every client use the same value, so they meet at the same place.
Why a Mismatched Port Breaks Connection
What do you think happens?
Suppose the server listens on one port number, but a client is set to a different port number. Does the connection request reach the Hub? Make a prediction first.
Reveal answer
No, the server and client do not meet on the same port
The stated requirement is that the server and all clients use the same port. A mismatched port breaks that requirement, so the fix is to align both sides on one shared value.
Mistakes Learners Make With Port Numbers
Common mistake Believing the port number must be a special or official value
Why it failsThe source says the value is arbitrary: any value can be chosen.
Do this insteadChoose any number, then make sure the server and every client use that same value.
Common mistake Changing the port on only one side
What it looks likeThe server listens on one number, and a client is set to another.
Why it failsThe server and clients must use the same port. A mismatch breaks the stated requirement.
Do this insteadKeep one shared PORT constant and make both programs use it.
Common mistake Thinking the server is a complicated program that handles every message
Why it failsThe source describes the server as a completely trivial program whose job is to create a Hub that listens for connection requests.
Do this insteadThink of the server as present and listening, and the clients as the ones who ask to connect.
Common mistake Assuming the Hub is a custom class written for this program
Why it failsThe chat room uses the Hub class itself, not a subclass of it.
Do this insteadTreat the Hub as the standard class the server creates directly.
Checking Your Understanding of the Setup
Without looking back, explain in your own words: (1) which program creates the netgame Hub, (2) what the Hub does while it waits, (3) what the clients send, and (4) why a port number that is not agreed on by both sides prevents a connection. Write one or two sentences per point.
Hints
- Recall that the server is the program named
ChatRoomServer.java. - The Hub listens for connection requests from netgame clients.
- Both sides must use the same PORT value, and the number itself is arbitrary.
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 itself, and that Hub listens for connection requests from netgame clients.
- The port number is a constant named PORT. Its value is arbitrary, but the server and every client must use the same value.
- A mismatched port breaks the stated requirement, so the fix is to align both sides on one shared value.