Java 1.1 Unleashed
-24 -
|
RFC Number | Topic |
791 | The Internet Protocol (IPv4) |
793 | The Transmission Control Protocol (TCP) |
768 | The User Datagram Protocol 2(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). Although it was written as a guide for systems 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).
IPng and the TCP/IP Protocols, by Stephan A. Thomas (John Wiley & Sons, ISBN 0-471-13088-5) offers an overview of version 6 of the Internet protocols.
The following sections give a short overview of the capabilities and limitations of the different network classes provided in the java.net package. If you have never done any network programming, these sections should help you decide the type of connection class on which you need to base your application. The overview can help you pick the Java classes that best fit your networking application. An overview of Java security, as it relates to network programming, is also provided.
The answer to this question depends on what you are trying to do and what type of application you are writing. Each network protocol has its own advantages and disadvantages. If you are writing a client for someone else's protocol, the decision probably has been made for you. If you are writing your own protocol from scratch, the following should help you decide which transport method (and hence, which Java classes) best fit your application.
The URL class is an example of what can be accomplished using the other, lower-level network objects. The URL class is best suited for applications or applets that have to access content on the World Wide Web. If all you use Java for is to write Web browser applets, the URL and URLConnection classes, in all likelihood, will handle your network communications needs.
The URL class enables you to retrieve a resource from the Web by specifying the Uniform Resource Locator for it. The content of the URL is fetched and turned into a corresponding Java object (such as a String containing the text of an HTML document). If you are fetching arbitrary information, the URLConnection object provides methods that try to deduce the type of the content either from the filename in the URL or from the content stream itself.
The Socket class provides a reliable, ordered stream connection (that is, a TCP/IP socket connection). The host and port number of the destination are specified when the Socket is created.
The connection is reliable because the transport layer (the TCP protocol layer) acknowledges the receipt of sent data. If one end of the connection does not receive an acknowledgment within a reasonable period of time, the other end re-sends the unacknowledged data (a technique known as Positive Acknowledgment with Retransmission, often abbreviated as PAR). Once you have written data into a Socket object, you can assume that the data will get to the other side (unless you receive an IOException, of course).
The term ordered stream means that the data arrives at the opposite end in the exact same order it is written. However, because the data is a stream, write boundaries are not preserved. What this means is that if you write 200 characters, the other side can read all 200 characters at once. But it might get the first 10 characters one time and the next 190 the next time data is received from the socket. In any case, the receiver cannot tell where each group of data was written.
The reliable stream connection provided by Socket objects is well suited for interactive applications. Examples of protocols that use TCP as their transport mechanism are Telnet and FTP. The HTTP protocol used to transfer data for the Web also uses TCP to communicate between hosts.
The ServerSocket class represents the thing with which Socket-type connections communicate. Server sockets listen on a given port for connection requests when their accept() method is called. The ServerSocket offers the same connection-oriented, ordered stream protocol (TCP) that the Socket object does. In fact, once a connection has been established, the accept() method returns a Socket object to talk with the remote end.
The DatagramSocket class provides an unreliable, connectionless, datagram connection (that is, a UDP/IP socket connection).
Unlike the reliable connection provided by a Socket, there is no guarantee that what you send over a UDP connection actually gets to the receiver. The TCP connection provided by the Socket class takes care of retransmitting any packets that get lost. Packets sent through UDP simply are sent out and forgotten, which means that if you need to know that the receiver got the data, you will have to send back some sort of acknowledgment. This arrangement does not mean that your data will never get to the other end of a UDP connection. If a network error happens (your cat jiggles the Ethernet plug out of the wall, for example), the UDP layer does not try to send it again or even know that the packet did not get to the recipient.
Connectionless means that the socket does not have a fixed receiver. You can use the same DatagramSocket to send packets to different hosts and ports; however, you can use a Socket connection to connect only to a given host and port. Once a Socket is connected to a destination, that destination cannot be changed. The fact that UDP sockets are not bound to a specific destination also means that the same socket can listen for packets as well as originate them. There is no UDP DatagramServerSocket equivalent to the TCP ServerSocket.
Datagram refers to the fact that the information is sent as discrete packets rather than as a continuous ordered stream. The individual packet boundaries are preserved. It may help to think of this process as dropping fixed-size postcards in a mailbox. If you send four packets, the order in which they arrive at the destination is not guaranteed to be the same as they were sent. The receiver may get them in the same order they were sent or the packets may arrive in reverse order. In any case, each packet is received whole.
Given the above constraints, why would anyone want to use a DatagramSocket? There are several advantages to using UDP:
The NFS (Network File System) protocol version 2, originally developed by Sun with implementations available for most operating systems, is an example of an application that uses UDP for its transport mechanism. Another example of an application in which a DatagramSocket may be appropriate is a multiplayer game. The central server must communicate with all the players involved and does not necessarily have to know that a position update got to the player.
NOTE: An actual game that uses UDP for communication is Netrek, a space combat simulation loosely based on the Star Trek series. Information on Netrek can be found using the Yahoo! subject catalog at this URL:
http://www.yahoo.com/Recreation/Games/Internet_Games/Netrek/
There is also a Usenet newsgroup for this game:
news:rec.games.netrek
Now that you know what the classes are capable of, you can choose the one that best fits your application. Table 24.2 sums up the type of connection each of the base networking classes creates. The Direction column indicates where a connection originates; Outgoing indicates that your application is opening a connection out to another host; Incoming indicates that some other application is initiating a connection to yours.
Class | Connection Type | Direction |
Socket | Connected, ordered byte stream (TCP) | Outgoing |
ServerSocket | Connected, ordered byte stream (TCP) | Incoming |
DatagramSocket | Connectionless datagram (UDP) | Incoming or Outgoing |
A common application for networking classes is the implementation of the client/server model. The client/server model is based on the idea that one computer specializing in information presentation displays the data stored and processed on a remote machine.
Today, the Internet provides home computers with the same networking power institutions outside the home have traditionally used. Many of the Internet applications you have come to know are client/server applications: the Web, e-mail, FTP, Telnet, and so on. Specifically, your home PC serves as the client side of the architecture. It displays information located on servers around the world.
The Web implements a simple form of client/server architecture for multiple client machines. Your computer, the client, uses a Web browser to display HTML documents stored somewhere on the Internet on a Web server.
There are four software components to the Web system:
The diagram in Figure 24.4 shows how this architecture fits together.
The client/server architecture of the Web.
The client/server architecture provides us with a logical breakdown of application processing. In an ideal environment, the server side of the application handles all common processing, and the client side handles user-specific processing. With the Web, the server stores the HTML documents shown to all the clients. Each client, on the other hand, has different display needs. Suppose that a user at a dumb terminal is limited to using the character-mode Lynx client. A Windows user, on the other hand, may have a GUI browser that uses the power of the graphical interface to display the document with full multimedia effects. The presentation of the HTML documents delivered by the server is thus left up to the client.
The two most common protocols for client/server communication are TCP/IP (Transmission Control Protocol/Internet Protocol) and UDP/IP (User Datagram Protocol/Internet Protocol). The choice of Java socket classes is dictated by the protocol you select for transmission because Socket objects are optimized for the underlying transmission protocol.
Now that you understand how to make computers talk to one another on the Internet with Java, it helps to understand how to design a client/server application that you want to build. As discussed earlier in this chapter, the client/server architecture assigns processing responsibility where it logically belongs. A simple system can be broken into two layers: a server (where data and common processing occurs) and a client (where user-specific processing occurs). This kind of architecture is more commonly known as a two-tier architecture. A simple time server is one example of a two-tier architecture.
Business applications--and, increasingly, Internet applications--are generally much more complex than simple two-tier applications. These more complex applications can involve relational databases and advanced server-side processing. Because client machines are becoming increasingly powerful, client/server development has enabled applications to move processing off the server and onto the client to facilitate the use of cheaper servers. This trend has led to a problem known as the problem of the fat client.
A fat client is a client in a client/server system that has absorbed an inordinate amount of the system's processing needs. Although a fat-client architecture is as capable as any other client/server configuration, it is harder to scale as your application grows over time. Using a common client/server tool such as PowerBuilder, your client application has direct knowledge of exactly how your data is stored and what it looks like in the data store (usually a database). If you ever change where that data is stored or how it is stored, you have to do significant rework of your client application.
The solution to the problem of the fat client is a three-tier client/server architecture that creates another layer of processing across the network. In Figure 24.5, you can see how the three-tier design divides application work into the following three tasks:
The three-tier client/server architecture.
One of the primary advantages of three-tier architecture is that, as your data storage needs grow, you can change the way data is stored without affecting your clients. The middle layer of the system, commonly referred to as the application server, can thus concentrate on centralizing business rule processing. (Business rule processing is the processing of data going to and from the clients in a way that is common to all clients.)
New technologies are on the horizon to help deliver you from the tedium of socket programming in a client/server environment. The most exciting of these technologies is distributed objects. A distributed application is a single application that has individual objects located on many machines. In an ideal world, these objects communicate with one another through simple method calls. Unfortunately, the ideal world is not here yet.
With the release of Java 1.1, Sun has provided a new API designed to allow you to distribute your Java applications. This new API, called Remote Method Invocation (RMI), enables a program on one machine to communicate with a program on another machine using simple Java method calls. Instead of writing a complex socket interface and an application-specific communication protocol, your application acts as though all the separate pieces were part of a single program on one machine. You call methods in any object, no matter where they exist, just as you call any other Java method.
A discussion of RMI is beyond the scope of this chapter (refer to Chapter 17, "The RMI Package," for more information). As a seamless method-based communications API, RMI provides an attractive alternative to writing socket code. Unfortunately, RMI works only when all the pieces of your application are Java pieces. In a hybrid system, sockets are still the best method of enabling communication among networked machines.
One of the purposes of Java is to enable executable content from an arbitrary network source to be retrieved and run securely. To accomplish this goal, the Java runtime system enforces certain limitations on what the classes obtained through the network can do. You should be aware of these constraints because they affect the design of applets and how the applets must be loaded. You must take into consideration the security constraints imposed by your target environment as well as your development environment when you design your application or applet.
For example, Netscape Navigator 2.0 allows code loaded from a local disk more privileges than code loaded over a network connection. A class loaded from an HTTP daemon may create only outgoing connections back to the host from which it was loaded. If the class is loaded from the local host (that is, if it is located somewhere in the class search path on the machine running Navigator), the class can connect to an arbitrary host. Contrast this with the applet viewer provided with Sun's Java Development Kit. The applet viewer can be configured to act in a way similar to Navigator or to enforce no restrictions on network connectivity.
If you require full access to all Java's capabilities, there is always the option of writing a standalone application. A standalone application (that is, one that does not run in the context of a Web browser) has no restrictions on what it can do. Sun's HotJava Web browser is an example of a standalone application.
NOTE: For a more detailed discussion of Java security and how it is designed into the language and runtime system, take a look at Chapter 34, "Java Security."
In addition, Sun has several white-paper documents and a collection of frequently asked questions available at http://www.javasoft.com/sfaq/.
These security checks are implemented by a subclass of java.lang.SecurityManager. Depending on the security model, the object allows or denies certain actions. You can check beforehand whether a capability your applet needs is present by calling the SecurityManager yourself. The java.lang.System object provides a getSecurityManager() method that returns a reference to the SecurityManager active for the current context. If your applet has to open a ServerSocket, for example, you can call the checkListen() method yourself and print an error message (or pop up a dialog box) alerting the users and referring them to installation instructions.
This chapter is a roadmap to the next three chapters. It has described the concepts you must 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). You also should have an idea of which Java class provides which functionality and how the Java classes fit into client/server architectures.
©Copyright, Macmillan Computer Publishing. All rights reserved.