Writing a Simple Network Server in Java
TCP/IP is the paired protocol arrangement behind Internet communication, and its TCP half provides reliable two-way communication.
Two Questions Before Any Message Moves
Suppose a chat program on your computer needs to exchange messages with a server running on another machine across the Internet. Before any message can move, the program has to answer a question that sounds simple: which computer, out of the enormous number reachable on the network, is the right one? Then it has to answer a second question: which of the many services running on that computer is the one it wants? Writing a network server in Java starts with these two answers, because a server has to be reachable at a known computer and listening on a known port.
This article follows the answers to both questions. It starts with the protocols that carry the data, moves to the addresses and names that identify a computer, and ends with the port number that selects a service and the connection that the two programs finally share.
Tracing a Chat Program's Connection
Think of the chat program as a client and the server as the program on the other machine. The client does not simply announce that it wants to talk to someone. It must specify a computer and a service on that computer. The computer is named by an IP address, or by a domain name that stands in for one. The service is named by a port number, which tells the machine which listening program the connection is aimed at.
Follow the identity step by step. First, the IP address picks out the machine. Second, the port number picks out the service on that machine. Together, those two values identify one specific endpoint. A connection between the client and the server is then identified by both values at once, so the same machine can carry several connections, each with its own port.
What do you think happens?
A Web server runs on a machine. Which port number does it generally listen on?
Reveal answer
80
The source gives the concrete case that a Web server generally listens for connections on port 80. Port 80 is below 1024, which places it in the range the source reserves for standard services.
What the TCP/IP Pair Provides
A protocol is an agreed-upon set of rules for how computers exchange data. TCP/IP is the paired arrangement of two protocols, the Transmission Control Protocol and the Internet Protocol, and it is the combination that most Internet communication relies on.
Communication over the Internet is built on these two protocols working together. When people say TCP/IP they usually mean the whole arrangement, not one protocol alone. The two halves do different jobs, and keeping them apart in your head makes the rest of the network material easier to follow.
Comparing the Two Halves
| Part | Main job | What a learner should remember |
|---|---|---|
| Internet Protocol | Identifies computers using addresses | Answers which computer to reach |
| Transmission Control Protocol | Provides reliable two-way communication | Makes data travel dependably in both directions |
| TCP/IP together | The paired arrangement most Internet communication relies on | Usually meant as one whole arrangement |
The two halves of TCP/IP and their roles, as described in the source
Reading an IPv4 Address
An IPv4 address is a 32-bit number written in dotted decimal form. Dotted decimal means the 32 bits are shown as four decimal numbers separated by dots. Each of the four numbers covers one 8-bit portion of the full address, so each can run from 0 to 255. Reading the address from left to right tells you the four portions in order.
The example address 192.0.2.10 is illustrative and was chosen for this article, not taken from the source. Each of its four parts is a decimal number from 0 to 255, and together the four parts make up the full 32 bits.
IPv6 addresses are larger. Where IPv4 uses 32 bits, IPv6 uses 128 bits, which is four times as many bits. The source describes this size difference but does not walk through IPv6 notation, so this article does not attempt to teach how IPv6 addresses are written.
Domain Names Standing In for Addresses
Numbers are efficient for machines but hard for people to remember. For that reason, many computers can be referred to by a domain name, a human-readable label such as math.hws.edu or www.whitehouse.gov. A domain name can be used instead of an IP address to say which computer you want.
A domain name and an IP address can both identify the same computer. The domain name is the human-friendly label, and the IP address is the numeric identifier it stands in for.
Ports Select the Service
An IP address is enough to reach a machine, but a machine can host many services at once. A server does not simply listen for connections in general. It listens on a particular port. A port number is a 16-bit positive integer, and it selects which service on the machine the connection is aimed at.
So before a client can connect, it must know two things: the Internet address or domain name of the computer where the server runs, and the port number on which that server is listening. Leaving out either one leaves the connection unidentified.
Working Through a Connection's Identity
Identifying two services on one machine
A machine at the generated address 192.0.2.10 runs a Web server on port 80 and a chat server on port 5000. A client wants to reach the chat server. What must the client specify, and which value picks out the service?
- 1Identify the computer
The IP address 192.0.2.10 picks out the machine. Both services share this address, so the address alone cannot distinguish them.
- 2Identify the service
The port number selects which service on that machine the connection is aimed at. Port 5000 is the chat server, and port 80 would be the Web server.
- 3Combine the two values
The client specifies the address and the port together. Those two values identify one specific endpoint on the machine.
The client specifies 192.0.2.10 and port 5000. The address names the machine, the port names the chat service, and a connection to that pair is distinct from a connection to port 80 on the same machine.
Mistakes About Addresses and Ports
Common mistake Thinking an IP address alone is enough to start talking to a server
What it looks likeA client knows only the address of a machine that runs both a Web server and a chat server.
Why it failsA machine can host many services, and the address only names the computer, not which service.
Do this insteadSpecify the port number as well, so the connection is aimed at the correct service.
Common mistake Treating a domain name and an IP address as two different computers
What it looks likeAssuming math.hws.edu must be a different machine from the one at its numeric address.
Why it failsA domain name can stand in for an IP address, so both can identify the same computer.
Do this insteadRead the domain name as a human-readable label for the same numeric identifier.
Common mistake Choosing a low port number for your own server program
What it looks likeSetting a custom server to listen on port 80 because a Web server uses it.
Why it failsPort numbers less than 1024 are reserved for particular standard services.
Do this insteadUse a port number greater than 1024 for your own server.
Common mistake Believing that one computer can carry only one connection at a time
Checking Your Understanding of Endpoints
A server program runs on a computer with the IPv4 address 192.0.2.10 and listens on port 8080. Explain what the IP address identifies, what the port number identifies, and why a second server on the same computer could use a different port without changing the address.
Hints
- Recall that the IP address picks out the computer and the port number picks out the service on that computer.
- Think about whether two services can share one IP address while still being distinct connections.
Key Takeaways
- TCP/IP is the paired arrangement of the Internet Protocol, which identifies computers, and the Transmission Control Protocol, which provides reliable two-way communication.
- An IPv4 address is a 32-bit number written in dotted decimal form, while an IPv6 address uses 128 bits.
- A domain name such as
math.hws.educan stand in for an IP address, so one computer can be reached by name or by number. - A network connection is identified by an IP address plus a port number; the port selects the service, and port numbers below 1024 are reserved for standard services, so your own server should use a port greater than 1024.