Concepts / Constants and Shared Values in Programs

Constants and Shared Values in Programs

The chat room application consists of two programs: a server, ChatRoomServer.java, and clients.

  • Programming

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.

  • One program that does everything

  • Two programs: a server and clients

  • Many programs, one for each message

Reveal answer
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.

ChatRoomServercreates a netgame HubNetgame Hublistens for connectionrequestsNetgame clientssend connection requestscreatesconnection request
What is each program responsible for, and how are the server and the clients connected to each other? The server creates the Hub, and the clients send their 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.

Key point

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.

ServerChatRoomServerNetgame HublisteningClientnetgame client1. create Hub2. connection request on the shared port
What happens step by step when a client asks to connect: who waits, who accepts, and what does the server do next? The server sets up the Hub first, the Hub waits, and then the client's request arrives on the shared port.
Worked example

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.

  1. 1
    Server creates the Hub

    The server program creates a netgame Hub. The Hub is the standard Hub class, not a subclass.

  2. 2
    Hub listens

    The Hub is now waiting for connection requests from netgame clients.

  3. 3
    Client 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.

  4. 4
    Stop at the request

    The source describes these steps only up to the connection request. What happens to messages after that is not covered here.

Result

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.

PORTone agreed port numberServerlistens on PORTClientsconnect to PORTusesuses
Where is the port number defined, and how do both programs read the same value without it being tied to any special meaning? Both sides refer to the same named constant, so they agree on one number.
Key point

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.

  • Yes, the Hub accepts any port

  • No, the server and client do not meet on the same port

Reveal answer
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 fails

    The source says the value is arbitrary: any value can be chosen.

    Do this instead

    Choose 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 like

    The server listens on one number, and a client is set to another.

    Why it fails

    The server and clients must use the same port. A mismatch breaks the stated requirement.

    Do this instead

    Keep 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 fails

    The source describes the server as a completely trivial program whose job is to create a Hub that listens for connection requests.

    Do this instead

    Think 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 fails

    The chat room uses the Hub class itself, not a subclass of it.

    Do this instead

    Treat the Hub as the standard class the server creates directly.

Checking Your Understanding of the Setup

EASY

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.