Concepts / Connecting a Client to a Server in Java

Connecting a Client to a Server in Java

TCP/IP is the paired protocol arrangement behind Internet communication, and its TCP half provides reliable two-way communication.

  • Programming

Two Questions Before Any Message Moves

Imagine a chat program on your computer that needs to exchange messages with a server running on another machine across the Internet. Before a single 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? Once that is answered, it has to answer a second question: which of the many services running on that computer is the one it wants? This article follows the answers to both questions. It starts with the protocols that carry the data and ends with the connection that the two programs finally share.

Keep these two questions in mind as you read. The first is answered by an IP address and, in many cases, a domain name. The second is answered by a port number. Together they identify a single connection.

Following the Two Questions Step by Step

Before looking at each piece in detail, trace the state a client program has to build up. Each step narrows down the target until one specific service on one specific computer is named.

  1. 1
    Step 1: The client knows a target. This is either an IP address or a domain name that stands in for one. The target says which computer to reach.
  2. 2
    Step 2: The target is one computer among all those reachable on the network. The IP address is the identifier that lets the network deliver data to that particular machine rather than another.
  3. 3
    Step 3: The client also needs a port number. A machine can host many services, so the port number says which service on that computer the connection is aimed at.
  4. 4
    Step 4: The server is listening on that port. The IP address and port number together identify the connection between client and server.
  5. 5
    Step 5: Because the port number is part of the identity, the same computer can carry several connections at once, each on its own port.

The Two Halves of TCP/IP

Definition

TCP/IP is the name for a pair of protocols: the Transmission Control Protocol and the Internet Protocol. Communication over the Internet is built on these two protocols working together. A protocol is an agreed-upon set of rules for how computers exchange data, and TCP/IP is the combination that most Internet communication relies on.

The two halves do different jobs, and it helps to keep them apart. The Internet Protocol is the part concerned with identifying computers, which is why the addressing discussion in this article spends so much time on addresses. The Transmission Control Protocol is the part that provides reliable two-way communication between networked computers. Reliable two-way communication means data can travel in both directions between the two machines in a dependable manner.

The two parts are paired. When people say TCP/IP, they usually mean the whole arrangement rather than one protocol alone. Addressing and delivery between machines belong to the Internet Protocol side, while dependable communication in both directions belongs to the Transmission Control Protocol side.

Key point

Internet Protocol: identifies computers. Transmission Control Protocol: provides reliable two-way communication. TCP/IP: the paired arrangement that most Internet communication depends on.

Reading an IPv4 Address

Every computer on the Internet has an IP address that identifies it. You can think of the IP address as the machine's identifier on the network. A client program needs some way to specify which computer, among all those reachable on the network, it intends to talk to, and the IP address is the mechanism for that. Without a unique identifier per machine, the network would have no way to deliver data to one particular computer rather than another.

IPv4 addresses are 32-bit addresses written in dotted decimal form. Thirty-two bits are split into four groups of eight bits. Each group is written as a decimal number, and the four numbers are separated by dots. Since eight bits can represent values from 0 to 255, each part of a dotted decimal IPv4 address falls in that range.

Worked example

Reading a dotted decimal IPv4 address

The address 192.168.0.1 is written in dotted decimal form. How many bits does it represent, and how is it divided?

  1. 1
    Count the groups

    The address has four numbers separated by dots: 192, 168, 0, and 1.

  2. 2
    Recognize the group size

    Each number stands for one group of eight bits. Four groups of eight bits make 32 bits in total, which matches the 32-bit size of an IPv4 address.

  3. 3
    Check the range of each number

    Each group of eight bits can hold a value from 0 to 255. All four numbers in this address fall inside that range, so the address is a valid dotted decimal IPv4 form.

Result

The address is 32 bits long, written as four decimal groups from 0 to 255 separated by dots. This example is an original illustration, not taken from the source.

IPv4 and IPv6 Compared

IPv4 uses 32-bit addresses, while IPv6 uses 128-bit addresses. The difference is one of size: an IPv6 address has four times as many bits as an IPv4 address. The source pack establishes the bit sizes but does not describe how IPv6 addresses are written on the page, so this article does not give an IPv6 notation.

IPv4 address32 bits, dotted decimalIPv6 address128 bits
How do IPv4 and IPv6 addresses differ in how many bits they contain?
PropertyIPv4IPv6
Size in bits32128
Written form named in sourceDotted decimalNot described in the source pack

Size and notation facts stated in the source pack

Domain Names and IP 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 name is the human-friendly way to refer to the machine, and the numeric address is what the network uses to identify it. The source establishes that the name stands in for the numeric address. It does not describe the mechanism by which a name is turned into an address, so this article does not explain that process.

Adding the Port Number

What do you think happens?

A web server is running on a machine that already has an IP address. Which additional value does a client need to reach that web server?

  • Port 80

  • Port 1500

  • Port 500

Reveal answer
Answer:

Port 80

The source states that a Web server generally listens for connections on port 80. The IP address alone reaches the machine, but the port number selects the web service on it.

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.

This means the client has to know two things before it can connect: the Internet address or domain name of the computer where the server runs, and the port number on which that server is listening. Drop either one and the target is incomplete. The address says where to go, and the port says which door to knock on.

IP addressidentifies the computerComputerruns many servicesConnectionclient and serverPort numberselects the serviceServer servicelistens on one portpicks outpicks outhostsincludesincludes
How does one computer running many network services let a connection reach exactly one of them, and what does the IP address contribute versus the port number?

The diagram shows the two halves of the identity. The IP address picks out the computer, and the port number picks out the particular service on that computer. A connection is identified by both at once. Because the port number is part of that identity, one computer can carry many connections at the same time, each on its own port. That is what allows several programs to communicate simultaneously.

Port Number Ranges

Standard Internet services have standard port numbers assigned to them. The standard port numbers are all less than 1024 and are reserved for particular services. The source's concrete case is the Web server, which generally listens on port 80.

If you write your own server program, the source advises using port numbers greater than 1024. That choice keeps your program clear of the range reserved for standard services.

Ports below 1024reserved for standardservicesPorts 1024 and aboveadvised for your own serverprograms
Which range of port numbers does the source assign to standard services, and where do the other ranges fall across the full port number span?

Mistakes Beginners Make with Addresses and Ports

  • Common mistake

    Thinking the IP address alone is enough to start a connection

    What it looks like

    A client knows the address of a machine that runs both a web server and a chat server, but gives no port number.

    Why it fails

    The IP address identifies the computer, not the particular service. The client also needs the port on which the target server is listening.

    Do this instead

    Supply both the address (or domain name) and the port number on which the server listens.

  • Common mistake

    Treating a domain name and an IP address as two different machines

    Why it fails

    The source says a domain name can stand in for an IP address, so both can identify the same computer.

    Do this instead

    Read a domain name as a human-friendly label for the same machine an IP address identifies.

  • Common mistake

    Assuming TCP/IP is a single protocol

    Why it fails

    TCP/IP names a pair: the Internet Protocol identifies computers and the Transmission Control Protocol provides reliable two-way communication.

    Do this instead

    Keep the two halves distinct and refer to the paired arrangement when discussing Internet communication.

  • Common mistake

    Choosing a port below 1024 for your own server program

    Why it fails

    Port numbers below 1024 are reserved for particular standard services according to the source.

    Do this instead

    Use a port number greater than 1024 for your own server.

  • Common mistake

    Assuming an IPv6 address is just a longer IPv4 address written the same way, when the source only establishes its 128-bit size

Checking Your Understanding of the Connection

EASY

A client program wants to reach a Web server on a computer whose domain name is math.hws.edu. Name the two values the client must know before it can connect, and explain why one computer can carry several connections at once.

Hints
  • Think about which value identifies the computer and which value identifies the service on it.
  • Recall what the source says about a domain name standing in for an IP address.
  • Consider what the port number contributes to the identity of each connection.
MEDIUM

You are writing your own server program. Which port number range does the source advise you to choose, and what would go wrong if you chose port 80 without a reason to run a Web server?

Hints
  • Compare your chosen number with the 1024 boundary.
  • The source reserves standard port numbers for particular services.

Key Takeaways

  • TCP/IP is the paired protocol arrangement behind Internet communication: the Internet Protocol identifies computers, and the Transmission Control Protocol provides reliable two-way communication.
  • An IP address identifies a computer; IPv4 uses 32-bit addresses written in dotted decimal form, and IPv6 uses 128-bit addresses. A domain name such as math.hws.edu can stand in for an IP address.
  • A network connection is identified by an IP address plus a port number. The IP address picks out the computer, and the port number selects the service, so one computer can carry many connections at once.
  • Standard service port numbers are all less than 1024 and reserved for particular services; a server you write should use a port number greater than 1024.