Java Network Programming, 3rd Edition Table of Contents Copyright Preface About the Third Edition Organization of the Book Who You Are Java Versions About the Examples Convention
Trang 2Java Network Programming, 3rd Edition
Table of Contents
Copyright
Preface
About the Third Edition
Organization of the Book
Who You Are
Java Versions
About the Examples
Conventions Used in This Book Request for Comments
Comments and Questions
Trang 42.6 Internet Standards
Chapter 3 Basic Web Concepts 3.1 URIs
3.2 HTML, SGML, and XML 3.3 HTTP
3.4 MIME Media Types
Trang 55.2 Returning Information from
Trang 67.1 The URL Class
7.2 The URLEncoder and
8.3 Parsing HTML
Trang 7Chapter 11 Secure Sockets 11.1 Secure Communications
Trang 811.2 Creating Secure Client Sockets
11.3 Methods of the SSLSocket Class
11.4 Creating Secure Server Sockets
Trang 9Chapter 13 UDP Datagrams and Sockets
13.1 The UDP Protocol
Trang 1014.3 Two Simple Examples Chapter 15 URLConnections 15.1 Opening URLConnections 15.2 Reading Data from a
Trang 1116.3 Writing a Protocol Handler 16.4 More Protocol Handler Examples and Techniques
Trang 1318.1 What Is Remote Method Invocation?
19.3 Receiving Mail
Trang 1419.4 Password Authentication 19.5 Addresses
19.6 The URLName Class 19.7 The Message Class
19.8 The Part Interface
19.9 Multipart Messages and File Attachments
Trang 15index_C index_D index_E index_F index_G index_H index_I index_J index_K index_L index_M index_N index_O index_P
Trang 16index_Q index_R index_S index_T index_U index_V index_W index_X index_Z
Trang 17Java Network Programming, 3rd Edition
By Elliotte Rusty Harold
Publisher : O'Reilly
Pub Date : October 2004
ISBN : 0-596-00721-3
Trang 18Pages : 706
Thoroughly revised to cover all the 100+ significant updates to Java Developers Kit (JDK) 1.5,
Java Network Programming is
a complete introduction to
developing network programs (both applets and applications) using Java, covering everything from networking fundamentals
Trang 19to remote method invocation (RMI) It includes chapters on TCP and UDP sockets,
multicasting protocol and
content handlers, servlets, and the new I/O API This is the essential resource for any
serious Java developer.
Trang 20Java Network Programming, 3rd Edition
By Elliotte Rusty Harold
Publisher : O'Reilly
Pub Date : October 2004
ISBN : 0-596-00721-3
Trang 21Pages : 706
Copyright
Preface
About the Third Edition
Organization of the Book
Who You Are
Java Versions
About the Examples
Conventions Used in This Book
Request for Comments
Comments and Questions
Acknowledgments
Chapter 1 Why Networked Java?
Section 1.1 What Can a Network Program
Do?
Section 1.2 Security
Trang 22Section 1.3 But Wait! There's More! Chapter 2 Basic Network Concepts Section 2.1 Networks
Section 2.2 The Layers of a Network Section 2.3 IP, TCP, and UDP Section 2.4 The Internet
Section 2.5 The Client/Server Model Section 2.6 Internet Standards Chapter 3 Basic Web Concepts
Section 3.1 URIs
Section 3.2 HTML, SGML, and XML Section 3.3 HTTP
Section 3.4 MIME Media Types Section 3.5 Server-Side Programs Chapter 4 Streams
Section 4.1 Output Streams
Section 4.2 Input Streams
Section 4.3 Filter Streams
Section 4.4 Readers and Writers Chapter 5 Threads
Trang 23Section 5.1 Running Threads
Section 5.2 Returning Information from a
Thread
Section 5.3 Synchronization
Section 5.4 Deadlock
Section 5.5 Thread Scheduling
Section 5.6 Thread Pools
Chapter 6 Looking Up Internet Addresses Section 6.1 The InetAddress Class
Section 6.2 Inet4Address and Inet6Address Section 6.3 The NetworkInterface Class Section 6.4 Some Useful Programs
Chapter 7 URLs and URIs
Section 7.1 The URL Class
Section 7.2 The URLEncoder and
URLDecoder Classes
Section 7.3 The URI Class
Section 7.4 Proxies
Section 7.5 Communicating with
Server-Side Programs Through GET
Section 7.6 Accessing Password-Protected
Trang 24Chapter 9 Sockets for Clients
Section 9.1 Socket Basics
Section 9.2 Investigating Protocols with
Telnet
Section 9.3 The Socket Class
Section 9.4 Socket Exceptions
Section 9.5 Socket Addresses
Section 9.6 Examples
Chapter 10 Sockets for Servers
Section 10.1 The ServerSocket Class Section 10.2 Some Useful Servers
Chapter 11 Secure Sockets
Section 11.1 Secure Communications Section 11.2 Creating Secure Client Sockets
Trang 25Section 11.3 Methods of the SSLSocket
Chapter 12 Non-Blocking I/O
Section 12.1 An Example Client
Section 12.2 An Example Server
Section 12.3 Buffers
Section 12.4 Channels
Section 12.5 Readiness Selection
Chapter 13 UDP Datagrams and Sockets Section 13.1 The UDP Protocol
Section 13.2 The DatagramPacket Class Section 13.3 The DatagramSocket Class Section 13.4 Some Useful Applications Section 13.5 DatagramChannel
Chapter 14 Multicast Sockets
Section 14.1 What Is a Multicast Socket?
Trang 26Section 14.2 Working with Multicast
Section 15.4 Configuring the Connection Section 15.5 Configuring the Client Request
HTTP Header
Section 15.6 Writing Data to a Server Section 15.7 Content Handlers
Section 15.8 The Object Methods
Section 15.9 Security Considerations for
Trang 27Section 16.1 What Is a Protocol Handler? Section 16.2 The URLStreamHandler Class Section 16.3 Writing a Protocol Handler Section 16.4 More Protocol Handler
Examples and Techniques
Section 16.5 The
URLStreamHandlerFactory Interface Chapter 17 Content Handlers
Section 17.1 What Is a Content Handler? Section 17.2 The ContentHandler Class Section 17.3 The ContentHandlerFactory
Interface
Section 17.4 A Content Handler for the
FITS Image Format
Chapter 18 Remote Method Invocation Section 18.1 What Is Remote Method
Invocation?
Section 18.2 Implementation
Section 18.3 Loading Classes at Runtime Section 18.4 The java.rmi Package
Trang 28Section 18.5 The java.rmi.registry Package Section 18.6 The java.rmi.server Package Chapter 19 The JavaMail API
Section 19.1 What Is the JavaMail API? Section 19.2 Sending Email
Section 19.3 Receiving Mail
Section 19.4 Password Authentication Section 19.5 Addresses
Section 19.6 The URLName Class
Section 19.7 The Message Class
Section 19.8 The Part Interface
Section 19.9 Multipart Messages and File
Trang 29Copyright © 2005, 2000, 1997 O'ReillyMedia, Inc All rights reserved.
Printed in the United States of America
Published by O'Reilly Media, Inc., 1005Gravenstein Highway North, Sebastopol,
Trang 30Nutshell Handbook, the Nutshell
Handbook logo, and the O'Reilly logo areregistered trademarks of O'Reilly Media,Inc Java™ Network Programming, ThirdEdition, the image of a river otter, andrelated trade dress are trademarks ofO'Reilly Media, Inc
Java™ and all Java-based trademarks andlogos are trademarks or registered
trademarks of Sun Microsystems, Inc., inthe United States and other countries.O'Reilly Media, Inc is independent of SunMicrosystems
Many of the designations used by
manufacturers and sellers to distinguishtheir products are claimed as trademarks
Trang 31Where those designations appear in thisbook, and O'Reilly Media, Inc was aware
of a trademark claim, the designationshave been printed in caps or initial caps
While every precaution has been taken inthe preparation of this book, the publisherand author assume no responsibility forerrors or omissions, or for damages
resulting from the use of the informationcontained herein
Trang 32Java's growth over the last 10 years hasbeen nothing short of phenomenal GivenJava's rapid rise to prominence and theeven more spectacular growth of the
Internet, it's a little surprising that networkprogramming in Java is still so mysterious
to so many It doesn't have to be In fact,writing network programs in Java is quitesimple, as this book will show Readerswith previous experience in networkprogramming in a Unix, Windows, orMacintosh environment should be
pleasantly surprised at how much easier it
is to write equivalent programs in Java.The Java core API includes well-designed
Trang 33interfaces to most network features.
Indeed, there is very little layer network software you can write in C
application-or C++ that you can't write mapplication-ore easily in
Java Java Network Programming, 3rd
Edition endeavors to show you how to
take advantage of Java's network classlibrary to quickly and easily write
programs that accomplish many commonnetworking tasks Some of these include:
Browsing the Web with HTTP
Parsing and rendering HTML
Sending email with SMTP
Trang 34Receiving email with POP and IMAP
Writing multithreaded servers
Installing new protocol and contenthandlers into browsers
Encrypting communications for
confidentiality, authentication, andguaranteed message integrity
Designing GUI clients for networkservices
Posting data to server-side programs
Looking up hosts using DNS
Trang 35Downloading files with anonymousFTP
Connecting sockets for low-levelnetwork communication
Distributing applications acrossmultiple systems with Remote
Method Invocation
Java is the first language to provide such apowerful cross-platform network library,which handles all these diverse tasks
Java Network Programming exposes the
power and sophistication of this library.This book's goal is to enable you to startusing Java as a platform for serious
network programming To do so, this book
Trang 36provides a general background in networkfundamentals, as well as detailed
discussions of Java's facilities for writingnetwork programs You'll learn how towrite Java programs that share data acrossthe Internet for games, collaboration,software updates, file transfer, and more.You'll also get a behind-the-scenes look atHTTP, SMTP, TCP/IP, and the otherprotocols that support the Internet and theWeb When you finish this book, you'llhave the knowledge and the tools to createthe next generation of software that takesfull advantage of the Internet
Trang 37About the Third Edition
In 1996, in the first chapter of the firstedition of this book, I wrote extensivelyabout the sort of dynamic, distributednetwork applications I thought Java wouldmake possible One of the most excitingparts of writing subsequent editions hasbeen seeing virtually all of the
applications I foretold come to pass.Programmers are using Java to querydatabase servers, monitor web pages,control telescopes, manage multiplayergames, and more, all by using Java's
native ability to access the Internet Java
in general and network programming inJava in particular has moved well beyondthe hype stage and into the realm of real,
Trang 38working applications Not all networksoftware is yet written in Java, but it's notfor a lack of trying Efforts are well underway to subvert the existing infrastructure
of C-based network clients and serverswith pure Java replacements Clients fornewer protocols like Gnutella and Freenetare preferentially written in Java It'sunlikely that Java will replace C for allnetwork programming in the near future.However, the mere fact that many peopleare willing to use web browsers, webservers, and more written in Java showsjust how far we've come since 1996
This book has come a long way, too Thethird edition has one completely newchapter to describe the most significant
Trang 39development in network programmingsince readers and writers were introduced
in Java 1.1 I refer of course to the newI/O APIs in the java.nio package Theability to perform asynchronous, non-blocking I/O operations is critical forhigh-performance network applications,especially servers It removes one of thelast barriers to using Java for networkservers Many other chapters have beenupdated to take advantage of these newI/O APIs
There've been lots of other small changesand updates throughout the java.net andsupporting packages in Java 1.4 and 1.5,and these are covered here as well Newclasses addressed in this edition include
Trang 40CookieHandler, SocketAddress, Proxy,NetworkInterface, and URI IPv6 hasbecome a reality, and is now coveredextensively Many other methods havebeen added to existing classes in the lasttwo releases of Java, and these are
discussed in the relevant chapters I'vealso rewritten large parts of the book toreflect changing fashions in Java
programming in general and networkprogramming in particular Applets andCGI programs are emphasized much less
In their place, you'll find more genericdiscussion of remote code execution andserver-side environments, however
implemented
Of course, the text has been cleaned up,
Trang 41too There's only one completely newchapter here, but the 18 existing chaptershave been extensively rewritten and
expanded to bring them up-to-date withnew developments as well as to makethem clearer and more engaging I hopeyou'll find this third edition an even
stronger, longer-lived, more accurate, andmore enjoyable tutorial and reference tonetwork programming in Java than the lastedition
Trang 42Organization of the Book
This book begins with three chapters thatoutline how networks and network
programs work Chapter 1, is a gentleintroduction to network programming inJava and the applications it makes
possible All readers should find
something of interest in this chapter Itexplores some of the unique programs thatbecome feasible when networking iscombined with Java Chapter 2, and
Chapter 3, explain in detail what a
programmer needs to know about how theInternet and the Web work Chapter 2
describes the protocols that underlie theInternet, such as TCP/IP and UDP/IP
Chapter 3 describes the standards that
Trang 43underlie the Web, such as HTTP, HTML,and REST If you've done a lot of networkprogramming in other languages on otherplatforms, you may be able to skip thesetwo chapters.
The next two chapters throw some light ontwo parts of Java programming that arecritical to almost all network programsbut are often misunderstood and misused,I/O and threading Chapter 4, exploresJava's classic I/O models which, despitethe new I/O APIs, aren't going away anytime soon and are still the preferred means
of handling input and output in most clientapplications Understanding how Javahandles I/O in the general case is a
prerequisite for understanding the special
Trang 44case of how Java handles network I/O.
Chapter 5, explores multithreading andsynchronization, with a special emphasis
on how they can be used for asynchronousI/O and network servers ExperiencedJava programmers may be able to skim orskip these two chapters However,
Chapter 6, is essential reading for
everyone It shows how Java programsinteract with the domain name systemthrough the InetAddress class, the oneclass that's needed by essentially allnetwork programs Once you've finishedthis chapter, it's possible to jump around
in the book as your interests and needsdictate There are, however, some
interdependencies between specific
chapters Figure P-1 should allow you to