Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Java Unleashed

Java Unleashed--ch27.htm Previous Page TOC Index Next Page

Chapter 27

Introducing Java network programming

One of the best features of Java is its networking support. Java has classes that range from low-level TCP/IP connections to ones that provide instant access to resources on the World Wide Web. Even if you have never done any network programming before, Java makes it easy.

The following chapters introduce you to the networking classes and how to use them. A guide to what is covered by each chapter follows:


Though networking with Java is fairly simple, there are a few concepts and classes from other packages that you should be familiar with before reading this part of the book. If you are only interested in writing an applet that interacts with an HTTP daemon, you probably can just concentrate on the URL class for now. For the other network classes, you will need at least a passing familiarity with the World Wide Web, classes, threads, and TCP/IP networking.

World Wide Web Concepts

If you are using Java you probably already have a familiarity with the Web. Knowledge of how Uniform Resource Locators (URLs) work is needed to use the URL and URLConnection classes. Classes

Once you have a network connection established using one of the low-level classes, you will be using and objects or appropriate subclasses of the objects to communicate with the other endpoint. Also, many of the classes throw when they encounter a problem.


Although not strictly needed for networking, threads make using the network classes easier. Why tie up your user interface waiting for a response from a server when a separate communication thread can wait rather than the interface thread? Server applications also can service several clients simultaneously by spawning off a new thread to handle each incoming connection.

TCP/IP Networking

Before using the networking facilities of Java, you need to be familiar with the terminology and concepts of the TCP/IP networking model. The last part of this chapter should serve to get you up to speed.

Internet Networking: A Quick Overview

TCP/IP (Transmission Control Protocol/Internet Protocol) is the set of networking protocols used by Internet hosts to communicate with other Internet hosts. If you have ever had any experience with networks or network programming in general you should be able to just skim this section and check back when you find a term you are not familiar with. A list of references is given at the end of this section if you would like more detailed information.

TCP/IP and Networking Terms

Like any other technical field, computer networking has its own set of jargon. These definitions should clear up what the terms mean.

The Internet Protocols

TCP/IP is a set of communications protocols for communicating between different types of machines and networks (hence the name internet). The name TCP/IP comes from two of the protocols: the Transmission Control Protocol and the Internet Protocol. Other protocols in the TCP/IP suite are the User Datagram Protocol (UDP), the Internet Control Message Protocol (ICMP), and the Internet Group Multicast Protocol (IGMP).

These protocols define a standard format for exchanging information between machines (known as hosts) regardless of the physical connections between them. TCP/IP implementations exist for almost every type of hardware and operating system imaginable. Software exists to transmit IP datagrams over network hardware ranging from modems to fiber-optic cable.

TCP/IP Network Architecture

There are four layers in the TCP/IP network model. Each of the protocols in the TCP/IP suite provides for communication between entities in one of these layers. These lower-level layers are used by higher-level layers to get data from host to host. The layers are as follows, with examples of what protocols live at each layer:

Figure FIGURE 27.1.

The TCP/IP protocol stack.

Each layer in the stack takes data from the one above it and adds the information needed to get the data to their destination, using the services of the layer below. One way to think of this layering is like the layers of an onion. Each protocol layer adds a layer to the packet going down the protocol stack. When the packet is received, each layer peels off its addressing to determine where to send the packet next.

As an example, suppose that your Web browser wants to retrieve something from a Web server running on a host on the same physical network. The browser sends an HTTP request using the TCP layer. The TCP layer asks the IP layer to send the data to the proper host. The IP layer then would use the physical layer to send the data to the appropriate host.

At the receiving end, each layer strips off the addressing information that the sender added and determines what to do with the data. Continuing the example, the physical layer would pass the received IP packet to the IP layer. The IP layer would determine that the packet is a TCP packet and pass it to the TCP layer. The TCP layer would pass the packet to the HTTP daemon process. The HTTP daemon then processes the request and sends the data requested back through the same process to the other host.

Figure FIGURE 27.2.

Addressing information is added and removed at each layer.

In a case where the hosts are not on the same physical network, the IP layer would handle routing the packet through the correct series of hosts (known as routers) until it reaches its destination. One of the nice features of the IP protocol is that individual hosts do not have to know how to reach every host on the Internet. The host simply passes to a default router any packets for networks it does not know how to reach.

For example, a university might only have one machine with a physical connection to the Internet. All of the campus routers would know to forward all packets destined for the Internet to this host. Similarly, any host on the Internet only has to know to get packets to this one router to reach any host at the university. The router would forward the packets to the appropriate local routers.

Figure FIGURE 27.3.

An example of IP routing.

There is a publicly available program for UNIX platforms called traceroute that is useful if you want to find out what routers actually are responsible for getting a packet from one host to another and how long each hop takes. The source for traceroute can be found by consulting an Archie server for an FTP site near you, or from

The Future: IP Version 6

Back when the TCP/IP protocols were being developed in the early 1970s, 32-bit IP numbers seemed more than capable of addressing all the hosts on an internet. Though there currently is not a lack of IP numbers, the explosive growth of the Internet in recent years is rapidly consuming the remaining unassigned addresses. To address this lack of IP numbers a new version of the IP protocols is being developed by the IETF. This new version, known as either IPv6 or IPng (IP Next Generation), will provide for a much larger address space of 128 bits. This address space will allow for approximately 3.4 x 1038 different IP addresses.

IPv6 will be backward compatible with current IP implementations to allow older clients to interoperate with newer ones. Other benefits of the new version are as follows:

These enhancements to TCP/IP should allow the Internet to continue the phenomenal growth it has experienced over the past few years.

Where to Find More Information

This was not meant to completely cover the subject of TCP/IP. If your curiosity has been piqued, the following online documents and books might be of interest to you.


The first and definitive source of information on the IP protocol family are the Request For Comments documents defining the standards themselves. An index of all of RFC documents is available through the Web at This page has pointers to all currently available RFCs (organized in groups of 100) as well as a searchable index.

Table 27.1 gives the numbers of some relevant RFCs and what they cover. Keep in mind that a given RFC might have been made obsolete by a subsequent RFC. The InterNIC site’s index will note in the description any documents that were made obsolete by a subsequent RFC.

Table 27.1. RFC documents.

RFC Number Topic
791 The Internet Protocol (IP)
793 The Transmission Control Protocol (TCP)
768 The User Datagram Protocol (UDP)
894 Transmission of IP Datagrams over Ethernet Networks
1171 The PPP Protocol
1883 IP Version 6
1602 The Internet Standards Process: How an RFC Becomes a Standard
1880 Current Internet Standards


A good introduction to TCP/IP is the book TCP/IP Network Administration by Craig Hunt (O’Reilly and Associates, ISBN 0-937175-82-X). Though written as a guide for system administrators of UNIX machines, the book contains an excellent introduction to all aspects of TCP/IP, such as routing and the Domain Name Service (DNS).

Another book worth checking out is The Design and Implementation of the 4.3BSD UNIX Operating System by Samuel J. Leffler et al. (Addison-Wesley, ISBN 0-201-06196-1). In addition to covering how a UNIX operating system works, it contains a chapter on the TCP/IP implementation.

If you are a beginner, another way to get started get started with TCP/IP is by reading Teach Yourself TCP/IP in 14 Days by Timothy Parker (Sams Publishing, ISBN 0-672-30549-6).


This chapter is a roadmap to the next three chapters. It has shown what concepts you need to be familiar with before you dive into network programming in Java. You should be comfortable with how TCP/IP networking operates in general (or at least know where to look for more information).

Previous Page TOC Index Next Page