This book is dedicated to my dog, Thor.
If you purchased this ebook directly from oreilly.com, you have the following benefits:
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Javas growth over the past 20 years has been nothing short of phenomenal. Given Javas rapid rise to prominence and the even more spectacular growth of the Internet, its a little surprising that network programming in Java remains so mysterious to so many. It doesnt have to be. In fact, writing network programs in Java is quite simple, as this book will show. Readers with previous experience in network programming in a Unix, Windows, or Macintosh environment will be pleasantly surprised at how much easier it is to write equivalent programs in Java. The Java core API includes well-designed interfaces to most network features. Indeed, there is very little application layer network software you can write in C or C++ that you cant write more easily in Java. Java Network Programming , Fourth Edition, endeavors to show you how to take advantage of Javas network class library to quickly and easily write programs that accomplish many common networking tasks. Some of these include:
Java is the first (though no longer the only) language to provide such a powerful cross-platform network library for handling all these diverse tasks. Java Network Programming exposes the power and sophistication of this library. This books goal is to enable you to start using Java as a platform for serious network programming. To do so, this book provides a general background in network fundamentals, as well as detailed discussions of Javas facilities for writing network programs. Youll learn how to write Java programs that share data across the Internet for games, collaboration, software updates, file transfer, and more. Youll also get a behind-the-scenes look at HTTP, SMTP, TCP/IP, and the other protocols that support the Internet and the Web. When you finish this book, youll have the knowledge and the tools to create the next generation of software that takes full advantage of the Internet.
About the Fourth Edition
In 1996, in the first edition of this books opening chapter, I wrote extensively about the sort of dynamic, distributed network applications I thought Java would make possible. One of the most exciting parts of writing subsequent editions has been seeing virtually all of the applications I foretold come to pass. Programmers are using Java to query database servers, monitor web pages, control telescopes, manage multiplayer games, and more, all by using Javas native ability to access the Internet. Java in general and network programming in Java in particular has moved well beyond the hype stage and into the realm of real, working applications.
This book has come a long way, too. The fourth edition focuses even more heavily on HTTP and REST. HTTP has gone from being one of many network protocols to almost the network protocol. As youll see, it is often the protocol on which other protocols are built, forming its own layer in the network stack.
There have been lots of other small changes and updates throughout the java.net
and supporting packages in Java 6, 7, and 8, and these are covered here as well. New classes addressed in this edition include CookieManager
, CookiePolicy
, CookieStore
, HttpCookie
, SwingWorker
, Executor
, ExecutorService
, AsynchronousSocketChannel
, AsynchronousServerSocketChannel
, and more. Many other methods have been added to existing classes in the last three releases of Java, and these are discussed in the relevant chapters. Ive also rewritten large parts of the book to reflect the ever-changing fashions in Java programming in general and network programming in particular. I hope youll find this fourth edition an even stronger, longer-lived, more accurate, and more enjoyable tutorial and reference to network programming in Java than the previous one.
Organization of the Book
, explains in detail what a programmer needs to know about how the networks and the Internet work. It covers the protocols that underlie the Internet, such as TCP/IP and UDP/IP.
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 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 InetAddress
class, the one class thats needed by essentially all network programs. Once youve finished this chapter, its possible to jump around in the book as your interests and needs dictate.
, explores Javas 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. Youll also learn about the newer URI
class, a more standards-conformant alternative for identifying but not retrieving resources.
, shows you how to use the URLConnection
and HttpURLConnection
classes not just to download data from web servers, but to upload documents and configure connections.
, shows you how to protect your client-server communications using the Secure Sockets Layer (SSL) and the Java Secure Sockets Extension (JSSE).
, introduces the new I/O APIs specifically designed for network servers. These APIs 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 dont help much for small servers or clients that dont open many simultaneous connections, but they may provide 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.