Chapter 10. Networking in Java - Client/Server Applications

Table of Contents

Java Internet Aware Applications
The InetAddress class
A Simple "Who Is?" Application
Java TCP Client Applications
The Finger Client
The Basic Web Browser
The TCP Client/Server
The Date Client/Server Overview
The Running Server/Client Applications
The Source Code
An Exercise For You. The Updated Client/Server Application


Historically, network programming has been very complicated and difficult. It was necessary to understand the various network layers involved along with the data packaging, network connection, data shipping and the handshaking involved. However, the concept of networking is not very difficult, quite similar to the concept of file I/O, except the file is being loaded and saved on some other computer. Java is network aware, with the layers of networking hidden from the programmer.

Each computer on the Internet has a unique (usually, unless connected within an Intranet) address called an IP address. Each IP address is a 32-bit number, of the form xx.xx.xx.xx, where xx is an 8-bit number that represents a number between 0 and 255. An IP address can also be represented by a domain name form, such as "". Computers also have a loopback IP address (, so that provided you have a TCP/IP stack installed on your computer you can develop network aware applications. The new IP address format is also available under Windows 2000 (IP-6) that provides addresses of the form xxxx.xxxx.xxxx.xxxx i.e. 128-bit addressing. IPv4 supports 4.2 billion addresses, which is inadequate to even provide one address to each living person (although routed internal network sharing and resuse is possible). IPv6 supports 3.4x10^38 addresses, enough to provide every atom of every person alive on the earth 7 unique IP addresses (assuming 6.5 billion humans, each having 7x10^27 atoms [12]).

A computer connected to the Internet, will often provide several services. For example, it could act as both a web server and an e-mail server. To allow multiple services, a port number is available, that provides a software abstraction, rather than a hardware address. Some common port for services are 80 (http), 21/22 (ftp) etc...

There are several mechanisms that provide network services. There is UDP (Unreliable Datagram Protocol) and TCP (Transfer-Control Protocol). We will use TCP for the majority of our applications, as we are guaranteed that the messages will arrive at their destination. UDP can often loose messages in transit, but is still useful for applications like broadcasting audio/video. Most protocols (such as HTTP, SMTP, POP and FTP) are based on TCP.

[12] Source: