Java Network Programming Other Java™ resources from O’Reilly Related titles Enterprise JavaBeans™ Java™ Programming with Java™ and XML Oracle SQLJ Java™ Cookbook™ Java™ Security Java™ Enterprise in a Nutshell JavaServer™ Pages Java™ I/O Java™ Swing Java™ in a Nutshell Learning Java™ Java™ Performance Tuning Java Books java.oreilly.com is a complete catalog of O’Reilly’s books on Resource Center Java and related technologies, including sample chapters and code examples. OnJava.com is a one-stop resource for enterprise Java develop- ers, featuring news, code recipes, interviews, weblogs, and more. Conferences O’Reillybringsdiverseinnovatorstogethertonurturetheideas thatsparkrevolutionaryindustries.Wespecializeindocument- ing the latest tools and systems, translating the innovator’s knowledgeintousefulskillsforthoseinthetrenches.Visitcon- ferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searchesacrossmorethan1,000books.Subscriberscanzeroin on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today with a free trial. THIRD EDITION Java Network Programming Elliotte Rusty Harold Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Java™ Network Programming, Third Edition by Elliotte Rusty Harold Copyright © 2005, 2000, 1997 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions arealsoavailableformosttitles(safari.oreilly.com).Formoreinformation,contactourcorporate/insti- tutional sales department: (800) 998-9938 [email protected]. Editor: Mike Loukides Production Editor: Colleen Gorman Cover Designer: Emma Colby Interior Designer: David Futato Printing History: February 1997: First Edition. August 2000: Second Edition. October 2004: Third Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’ReillyMedia,Inc.Java™NetworkProgramming,ThirdEdition,theimageofariverotter,andrelated trade dress are trademarks of O’Reilly Media, Inc. Java™ and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems,Inc.,intheUnitedStatesandothercountries.O’ReillyMedia,Inc.isindependentofSun Microsystems. Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedas trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. Whileeveryprecautionhasbeentakeninthepreparationofthisbook,thepublisherandauthorassume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-0-596-00721-8 [LSI] [2011-07-01] To Grandmama, a great grandmother. Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Why Networked Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Can a Network Program Do? 2 Security 16 But Wait! There’s More! 18 2. Basic Network Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Networks 19 The Layers of a Network 21 IP, TCP, and UDP 27 The Internet 31 The Client/Server Model 36 Internet Standards 38 3. Basic Web Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 URIs 46 HTML, SGML, and XML 53 HTTP 55 MIME Media Types 59 Server-Side Programs 63 4. Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Output Streams 68 Input Streams 72 Filter Streams 76 Readers and Writers 91 vii 5. Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Running Threads 107 Returning Information from a Thread 111 Synchronization 123 Deadlock 129 Thread Scheduling 130 Thread Pools 144 6. Looking Up Internet Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 The InetAddress Class 152 Inet4Address and Inet6Address 169 The NetworkInterface Class 170 Some Useful Programs 173 7. URLs and URIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 The URL Class 184 The URLEncoder and URLDecoder Classes 208 The URI Class 213 Proxies 222 Communicating with Server-Side Programs Through GET 225 Accessing Password-Protected Sites 229 8. HTML in Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 HTML on Components 237 JEditorPane 239 Parsing HTML 248 Cookies 266 9. Sockets for Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Socket Basics 275 Investigating Protocols with Telnet 276 The Socket Class 278 Socket Exceptions 304 Socket Addresses 305 Examples 306 10. Sockets for Servers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 The ServerSocket Class 325 Some Useful Servers 342 viii | Table of Contents