18. Sockets and Networking

18.2. An Overview of Networks

Networking is a broad and complex topic. In a typical computer science curriculum, it is covered in one or more upper-level courses. Neverthe- less, in this chapter you can learn enough about networking to be able to use network resources and to design simple Java networking applications.

Network Size and Topology

Computer networks come in a variety of sizes and shapes. A local area network (LAN) is usually a privately owned network located within a sin- gle office or a single organization. Your campus network would be an example of a LAN. A wide area network (WAN) spans a wide geographical distance like a country or a continent. It may use a combination of public, private, and leased communication devices. Some of the large commercial networks, such as MCI and Sprint, are examples of WANs.

The computers that make up a network can be arranged in a variety of topologies, or shapes, some of the most common of which are shown in Fig- ures 15.1 and 15.2. As you would expect, different topologies use different techniques for transmitting information from computer to computer.

 

 

 

Star topologyBus topology

Ring topology

 

In a star network (Fig. 15.1), a central computer functions as a hub, with every other computer in the network connected to the hub. Each

 

Figure 15.2: Tree and fully con- nected mesh topologies.

 

 

 

 

 

 

 

 

Tree topologyFully connected mesh topology

 

 

 

 

computer can communicate with the others but only through the hub. The bus topology doesn’t have a hub computer. Instead, each node looks at each message sent on the bus to find those that are addressed to it. In sending a message, a node waits until the bus is free and then transmits the message.

A ring network (Fig. 15.1) also has no host, and the computers are con- nected in a loop, through which they exchange information. The tree topology (Fig. 15.2) is organized into a hierarchy, with each level (trunk of the tree, major branch of the tree) controlled by a hub. The fully con- nected mesh network directly connects all points to all points, eliminat- ing the “middleman.” Here there is no need to go through one or more other computers in order to communicate with a particular computer in the network.

Network topologies differ quite a bit in the expense of the wiring they require, their efficiency, their susceptibility to failure, and the types of protocols they use. These differences are beyond the scope of this chapter.

 

Internets

 

An internet (lowercase i) is a collection of two or more distinct networks, joined by devices called routers (Fig. 15.3). An internet is like a meeting of

the United Nations. Each country sends a delegation, all of whose mem-An internet vs. the Internet

bers speak that country’s language. A national delegation is like a single computer network. Language interpreters take on the task of translating one language to another so that any two delegations, say, the United States and China, can communicate. The routers play a similar translation role within an internet. The UN conference, composed of communicating del- egations from all the different countries of the world, is like a worldwide internet.

The United Nations is an apt analogy for the Internet (uppercase I), which is an example of a particular worldwide internet. Internets, in the generic sense, shouldn’t be confused with the Internet. It’s quite likely that your campus LAN is itself composed of several, smaller networks, each of which uses its own “language.”

 

Figure 15.3: An internet is a col- lection of distinct networks joined together by routers.

 

 

 

 

 

 

 

 

 

 

 

 

 

SELF-STUDY EXERCISES

EXERCISE 15.1In a network of ten computers, which topology would require the most cables?

 

EXERCISE 15.2Which topology would be most resistant to having one of its computers crash?

 

 

 

 

 

 

 

 

Network protocols


EXERCISE 15.3 Which topology would be least resistant to having one of its computers crash?

Network Protocols

A protocol is a set of rules that governs the communication of infor- mation. For example, the World Wide Web is based on the HyperText Transfer Protocol (HTTP). HTTP describes how information is to be ex- changed between a Web browser, such as Internet Explorer or Netscape Navigator, and a Web server, which stores an individual’s or company’s Web pages. Web pages are encoded in the HyperText Markup Language (HTML). Among other things, the HTTP protocol can interpret HTML pages.

Similarly, the Simple Mail Transfer Protocol (SMTP) is a set of rules that governs the transfer of e-mail. And the File Transfer Protocol (FTP) is the protocol that governs the transfer of files across the Internet.

 

Application Protocols

These three examples—HTTP, SMTP, and FTP—are examples of applica- tion protocols. They are relatively high-level protocols that support and govern a particular network application, such as e-mail or WWW ac- cess. Among the things they determine how we address different com- puters on the network. For example, the HTTP protocol specifies Web addresses by using a Uniform Resource Locator (URL). A URL speci- fies three necessary bits of information: The method used to transfer in- formation (e.g., HTTP or FTP), the address of the host computer (e.g.,

 

www.prenhall.com), and the path describing where the file is located on the host ( /morelli/index.html):

,,

 

J

Similarly, an e-mail address is specified by the SMTP protocol to consist of a local mailbox address (George.W.Bush) followed by the address of the computer (mail.whitehouse.gov):

,,

 

J

Another good example of an application protocol is the Internet’s Domain Name System (DNS), which is the system that governs how names, such

as whitehouse.gov and java.trincoll.edu, can be translated into Internet domain names

numeric addresses. In the DNS, each host computer on the Internet is identified with a unique host name—for example, mail, java—which is usually made up by the network administrator whose job it is to man- age an organization’s network. The DNS divides the entire Internet into a hierarchy of domains and subdomains. The generic domains are names like com, edu, and mil, which refer to the type of organization— com- mercial, educational, and military, respectively. In addition to these there are country domains, such as fr, au, and nz, for France, Australia, and New Zealand. Finally, individuals and organizations can buy their own domain names, such as whitehouse, microsoft, and trincoll.

What makes the whole system work is that certain computers within

the network are designated as DNS servers. It is their role to translate names such as java.trincoll.edu to numeric addresses whenever they are requested to do so by clients such as the SMTP or the HTTP server. Also, the DNS servers must communicate among themselves to make sure that their databases of names and addresses are up-to-date.

 

SELF-STUDY EXERCISE

EXERCISE 15.4 What’s the URL of the Web server at Prentice Hall? Identify its component parts—host name, domain name, Internet domain.

Client/Server Applications

The HTTP, FTP, SMTP, and DNS protocols are examples of client/server

 

protocols, and the applications they support are examples of client/server applications. In general, a client/server application is one in which the task at hand has been divided into two subtasks, one performed by the client and one performed by the server (Fig. 15.4).


Request service

 

Provide service

 

For example, in the HTTP case, the Web browser plays the role of a

client by requesting a Web page from a Web (HTTP) server. A Web server

 

is just a computer that runs HTTP software—a program that implements the HTTP protocol. For e-mail, the program you use to read your e-mail— Eudora, Pine, or Outlook—is an e-mail client. It requests certain services, such as send mail or get mail, from an e-mail (SMTP) server, which is simply a computer that runs SMTP software. In the FTP case, to transfer


Figure 15.4: Client/server appli- cation.

 

a program from one computer to another, you would use an FTP client, such as Fetch. Finally, in the DNS case, the DNS servers handle requests for name to address translations that come from HTTP, FTP, and SMTP servers, acting in this case like clients.

So we can say that a client/server application is one that observes the following protocol:

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 15.5: Levels of network protocols.

 

Packet transfer

 

 

 

 

 

 

 

 

Disparate protocols


J

As these examples illustrate, many Internet applications are designed as client/server applications.

 

JAVA EFFECTIVE DESIGN

Divide and Conquer. The client/server

protocol is an example of the effective use of the divide-and-conquer strategy.

 

SELF-STUDY EXERCISE

EXERCISE 15.5 Lots of our everyday interactions fit into the client/ser- ver model. Suppose you are the client in the following services:

Buying a piece of software at a bookstore.

Buying a piece of software over the phone.

Buying a piece of software over the Internet. Identify the server and then describe the basic protocol.

Lower Level Network Protocols

Modern computer networks, such as the Internet, are organized into a number of levels of software and hardware. Each level has its own collec- tion of protocols (Fig. 15.5).

The application level, which contains the HTTP, FTP, SMTP, and DNS protocols, is the highest level. Underlying the application-level protocols are various transmission protocols, such as the Transfer Control Protocol (TCP) and the User Datagram Protocol (UDP). These protocols govern the trans- fer of large blocks of information, or packets, between networked com- puters. All of the applications we mentioned—WWW, e-mail, and file transfer— involve data transmission and, therefore, rely on one or more of the transmission protocols.

At the very lowest end of this hierarchy of protocols are those that govern the transmission of bits or electronic pulses over wires and those that govern the delivery of data from node to node. Most of these proto- cols are built right into the hardware—the wires, connectors, transmission devices—that networks use. On top of these are protocols, such as the ethernet protocol and token ring protocol, that govern the delivery of pack- ets of information on a local area network. These too may be built right into the network hardware.

As you might expect, these lower level protocols are vastly different from each other. An ethernet network cannot talk directly to a token ring

 

 

Ethernet-based LANToken-ring based LAN


Figure 15.6: Routers between indi- vidual networks use the IP proto- col to translate one network pro- tocol to another.

 

 

 

 

 

 

English protocol


Router (IP protocol)


French protocol

 

 

 

 

network. How can we connect such disparate networks together? Think again of our United Nations analogy. How do we get French-speaking net- works to communicate with English-speaking networks? The answer sup- plied by the Internet is to use the Internetworking Protocol (IP), which governs the task of translating one network protocol to a common format (Fig. 15.6).

To push the UN analogy a bit further, the Internet’s IP is like a univer- The Internet protocol

sal language built into the routers that transmit data between disparate networks. On one end of a transmission, a router takes a French packet of information received from one of the delegates in its network. The router translates the French packet into an IP packet, which it then sends on through the network to its destination. When the IP packet gets close to its destination, another router takes it and translates it into an English packet before sending it on to its destination on its network.

 

The java.net Package

As we have seen, networks are glued together by a vast array of protocols. Most of these protocols are implemented in software that runs on general- purpose computers. You can install software on your personal computer to turn it into a Web server, an FTP server, or an e-mail server. Some of the lower level protocols are implemented in software that runs on special- purpose computers, the routers. Still other protocols, such as the ethernet protocol, are implemented directly in hardware.

Fortunately, we don’t have to worry about the details of even the high- est level protocols in order to write client/server applications in Java. The java.net (Fig. 15.7) package supplies a powerful and easy-to-use set of classes that supports network programming.

The java.net.URL class provides a representation of the Internet’s java.net.*

Uniform Resource Locator that we described earlier. We’ll show how to

use its methods to download WWW pages. We’ll also look at an example that uses a URL and an input stream so that files stored on the Web can be used as input files to a Java applet or application program.

The Socket and ServerSocket classes provide methods that let us develop our own networking applications. They enable us to make a direct connection to an Internet host, and read and write data through InputStreams and OutputStreams. As we will see, this is no more difficult than reading and writing data to and from files. The DatagramPacket and DatagramSocket classes provide support for even lower-level networking applications, based on Internet packets.

 

Figure 15.7: The java.net pack- age.