Organization of the Book
This tutorial begins with three chapters that outline how networks and network programs work. , is a gentle introduction to network coding in Java and the apps it makes possible. All readers should find something of interest in this chapter. It explores some of the unique programs that become feasible when networking is combined with Java. , and , explain in detail what a programmer needs to know about how the Internet and the Web work. describes the protocols that underlie the Internet, such as TCP/IP and UDP/IP. describes the standards that underlie the Web, such as HTTP, HTML, and REST. If you've done a lot of network coding in other languages on other platforms, you may be able to skip these two chapters. The next two chapters throw some light on two parts of Java programming that are critical to almost all network programs but are often misunderstood and misused, I/O and threading. , explores Java's classic I/O models which, despite the new I/O APIs, aren't going away any time soon and are still the preferred means of handling input and output in most client apps. Understanding how Java handles I/O in the general case is a prerequisite for understanding the special case of how Java handles network I/O. , explores multithreading and synchronization, with a special emphasis on how they can be used for asynchronous I/O and network servers. Experienced Java programmers may be able to skim or skip these two chapters. However, , is essential reading for everyone. It shows how Java programs interact with the domain name system through the , URLs and URIs, explores Java's InetAddress
class, the one class that's needed by essentially all network programs. Once you've finished this chapter, it's possible to jump around in the tutorial as your interests and needs dictate. There are, however, some interdependencies between specific chapters. Figure P-1 should allow you to map out possible paths through the tutorial.
Screenshot P-1. Chapter prerequisites
URL
class, a powerful abstraction for downloading information and files from network servers of many kinds. The URL
class enables you to connect to and download files and documents from a network server without concerning yourself with the details of the protocol the server speaks. It lets you connect to an FTP server using the same code you use to talk to an HTTP server or to read a file on the local hard disk. Once you've got an HTML file from a server, you're going to want to do something with it. Parsing and rendering HTML is one of the most difficult challenges network programmers can face. , introduces some little known classes for parsing and rendering HTML documents that take this burden off your shoulders and put it on Sun's. through discuss Java's low-level socket classes for network access. , introduces the Java sockets API and the Socket
class in particular. It shows you how to write network clients that interact with TCP servers of all kinds including whois, finger, and HTTP. , shows you how to use the ServerSocket
class to write servers for these and other protocols in Java. , shows you how to protect your client server communications using the Secure Sockets Layer (SSL) and the Java Secure Sockets Extension (JSSE). , covers the new I/O APIs introduced in Java 1.4. These APIs were specifically designed for network servers. They enable a program to figure out whether a connection is ready before it tries to read from or write to the socket. This allows a single thread to manage many different connections simultaneously, thereby placing much less load on the virtual machine. The new I/O APIs don't help much for small servers or clients that don't open many simultaneous connections, but they provide huge performance boosts for high volume servers that want to transmit as much data as the network can handle as fast as the network can deliver it. , introduces the User Datagram Protocol (UDP) and the associated DatagramPacket
and DatagramSocket
classes that provide fast, unreliable communication. Finally, , shows you how to use UDP to communicate with multiple hosts at the same time. All the other classes that access the network from Java rely on the classes described in these five chapters. through look more deeply at the infrastructure supporting the URL
class. These chapters introduce protocol and content handlers, concepts unique to Java that make it possible to write dynamically extensible software that automatically understands new protocols and media types. , describes the class that serves as the engine for the URL
class of . It shows you how to take advantage of this class through its public API. , also focuses on the URLConnection
class but from a different direction; it shows you how to subclass this class to create handlers for new protocols and URLs. Finally, , explores Java's somewhat moribund mechanism for supporting new media types. and introduce two unique higher-level APIs for network programs, Remote Method Invocation (RMI) and the JavaMail API. , introduces this powerful mechanism for writing distributed Java apps that run across multiple heterogeneous systems at the same time while communicating with straightforward method calls just like a nondistributed program. , acquaints you with this standard extension to Java, which offers an alternative to low-level sockets for talking to SMTP, POP, IMAP, and other email servers. Both of these APIs provide distributed apps with less cumbersome alternatives to lower-level protocols.