Đây là bộ sách tiếng anh cho dân công nghệ thông tin chuyên về bảo mật,lập trình.Thích hợp cho những ai đam mê về công nghệ thông tin,tìm hiểu về bảo mật và lập trình.
www.it-ebooks.info www.it-ebooks.info Java Network Programming www.it-ebooks.info Other Java ™ resources from O’Reilly Related titles Enterprise JavaBeans ™ Java ™ and XML Java ™ Cookbook ™ Java ™ Enterprise in a Nutshell Java ™ I/O Java ™ in a Nutshell Java ™ Performance Tuning Java ™ Programming with Oracle SQLJ Java ™ Security JavaServer ™ Pages Java ™ Swing Learning Java ™ Java Books Resource Center java.oreilly.com is a complete catalog of O’Reilly’s books on 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’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit con- 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 searches across more than 1,000 books. Subscribers can zero in 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. www.it-ebooks.info Java Network Programming THIRD EDITION Elliotte Rusty Harold Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info 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 are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti- tutional sales department: (800) 998-9938 or corporate@oreilly.com. 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’Reilly Media, Inc. Java ™ Network Programming, Third Edition, the image of a river otter, and related 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., in the United States and other countries. O’Reilly Media, Inc. is independent of Sun Microsystems. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as 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. While every precaution has been taken in the preparation of this book, the publisher and author assume 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] www.it-ebooks.info To Grandmama, a great grandmother. www.it-ebooks.info www.it-ebooks.info vii 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 www.it-ebooks.info viii | Table of Contents 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 www.it-ebooks.info [...]... write Java 2 SDK, Standard Edition, v1.3 or even Java 2 1.3 every time I want to point out a new feature in the latest release of Java I normally simply refer to Java 1.1, Java 1.2, Java 1.3, Java 1.4, and Java 1.5 Overall, though, the networking API seems fairly stable Java 1.1 through Java 1.3 are very similar, and there are a few only major additions in Java 1.4 and 1.5 Very little of the post-1.0 networking... stronger, longer-lived, more accurate, and more enjoyable tutorial and reference to network programming in Java than the last edition Organization of the Book This book begins with three chapters that outline how networks and network programs work Chapter 1, Why Networked Java? , is a gentle introduction to network programming in Java and the applications it makes possible All readers should find something... 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 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 Java s facilities for writing network programs... experience in network programming in a Unix, Windows, or Macintosh environment should 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 can’t write more easily in Java Java Network Programming, 3rd... Preface Java s growth over the last 10 years has been nothing short of phenomenal Given Java s rapid rise to prominence and the even more spectacular growth of the Internet, it’s a little surprising that network programming in Java is still so mysterious to so many It doesn’t have to be In fact, writing network programs in Java is quite simple, as this book will show Readers with previous experience in network. .. web is going to entwine the network ever more deeply in all kinds of applications All of this will take place over the Internet and all of it can be written in Java Java was the first programming language designed from the ground up with networking in mind Java was originally designed for proprietary cable television networks rather than the Internet, but it’s always had the network foremost in mind One... how to write network applications in C++ But it’s not clear why you would want to: as I said earlier, Java lets you write very sophisticated applications with ease Java Versions Java s network classes have changed a lot more slowly since Java 1.0 than other parts of the core API In comparison to the AWT or I/O, there have been almost no changes and only a few additions Of course, all network programs... written in Java It’s unlikely that Java will replace C for all network programming in the near future However, the mere fact that many people are willing to use web browsers, web servers, and more written in Java shows just how far we’ve come since 1996 This book has come a long way, too The third edition has one completely new chapter to describe the most significant development in network programming. .. experience with network programming You should find it a complete introduction to networking concepts and network application development I don’t assume that you have a few thousand networking acronyms (TCP, UDP, SMTP, etc.) at the tip of your tongue You’ll learn what you need to know about these here It’s certainly possible that you could use this book as a general introduction to network programming. .. using a Java applet loaded into their web browsers Figure 1-5 WebCollab Peer-to-peer networked Java programs allow multiple people to collaborate on a document at one time Imagine a Java word processor that two people, perhaps in different countries, can both pull up and edit simultaneously More recently, the Java Media Framework 2.0 has added voice to the media that Java can transmit across the network, . Cookbook ™ Java ™ Enterprise in a Nutshell Java ™ I/O Java ™ in a Nutshell Java ™ Performance Tuning Java ™ Programming with Oracle SQLJ Java ™ Security JavaServer ™ . simply refer to Java 1.1, Java 1.2, Java 1.3, Java 1.4, and Java 1.5. Overall, though, the networking API seems fairly stable. Java 1.1 through Java 1.3 are