Table of Contents

preface xxiii
preface to the first edition xxv
acknowledgments xxvii
guide to the reader xxix
about the cover illustration xxxiiii

Part I Preliminaries

1 Introduction to networking 3

1.1 Stacks and layers 4

1.2 The Internet 6

1.3 TCP: transmission control protocol 9

1.4 UDP: user datagram protocol 11

1.5 The IP family of services 12

1.6 DNS 14

1.7 Firewalls and proxy servers 15

1.8 Conclusion 16

2 The Java security model 17

2.1 Untrusted and trusted applets 18

2.2 SecurityManager and resources 19

2.3 Networked applets 21

2.4 Firewalls 23

2.5 The security API 25

2.6 Conclusion 25

3 An overview of exceptions 27

3.1 Exception catching 28

3.2 The major types of exceptions 31

3.3 Generating exceptions 29

3.4 Handling exceptions 29

3.5 Class Throwable 33

3.6 User exceptions 34

3.7 Wrapping up 34

4 An overview of multithreading 35

4.1 Variable scope in threads 36

4.2 Synchronization 37

4.3 Class Thread 41

4.4 Interface Runnable 45

4.5 Class ThreadGroup 45

4.6 A Runnable example 46

4.7 A Thread subclass example 48

4.8 Thread notification methods 49

4.9 A producer-consumer example 50

4.10 Efficient multithreading 52

4.11 Wrapping up 55

Part II Streams

5 An introduction to streams 61

5.1 Overview 62

5.2 Character Streams 63

5.3 Class OutputStream 64

5.4 A simple OutputStream example 66

5.5 Class InputStream 69

5.6 A simple InputStream example 72

5.7 Clsss Tee example 73

5.8 Basic stream types 76

5.9 Wrapping up 77

6 File access through streams 79

6.1 Class File 80

6.2 Class FileDescriptor 84

6.3 Class RandomAccessFile 85

6.4 Class FileOutputStream 89

6.5 Class FileInputStream 91

6.6 A simple file streams example 92

6.7

Writing an overwriting FileOutputStream 93

6.8 Writing a seekable FileOutputStream 95

6.9 Building a mark/reset FileInputStream 97

6.10 Wrapping up 99

7 Extending streams with filters 101

7.1 Providing higher level communications 102

7.2 Class FilterOutputStream 104

7.3 Class FilterInputStream 105

7.4 Standard stream filters 106

7.5 Using stream filters 108

7.6 Building text stream filters 110

7.7 Wrapping up 117

8 Supplied stream filters 121

8.1 Class DataOutputStream 120

8.2 Class DataInputStream 123

8.3 Class BufferedOutputStream 125

8.4 Class BufferedInputStream 127

8.5 Class PrintStream 128

8.6 Class SequenceInputStream 130

8.7 Class LineNumberInputStream 131

8.8 Class PushbackInputStream 132

8.9 Writing a buffered input stream 135

8.10 Building a tee stream filter 138

8.11 Wrapping up 145

9 Memory I/O streams 147

9.1 Class ByteArrayOutputStream 148

9.2 A simple ByteArrayOutputStream example 150

9.3 Class ByteArrayInputStream 151

9.4 A simple ByteArrayInputStream example 152

9.5 Class PipedOutputStream 153

9.6 Class PipedInputStream 154

9.7 A piped stream example 155

9.8 An autoresetting ByteArrayOutputStream 157

9.9 Wrapping up 159

10 Character streams 161

10.1 Overview 162

10.2 Character encoding 164

10.3 Class Writer 167

10.4 Class Reader 169

10.5 Class OutputStreamWriter 171

10.6 Class InputStreamReader 173

10.7 An encoding converter 174

10.8 Class FileWriter 175

10.9 Class FileReader 176

10.10 Using the file streams 177

10.11 Wrapping up 178

11 Character stream filters 181

11.1 Class FilterWriter 182

11.2 Class FilterReader 183

11.3 Class BufferedWriter 185

11.4 Class BufferedReader 186

11.5 Class LineNumberReader 187

11.6 Using character stream filters 187

11.7 Class PrintWriter 189

11.8 Class PushbackReader 190

11.9 Building a textual data Reader 191

11.10 LineAtATimeReader 197

11.11 Wrapping up 201

12 Memory-based character streams 203

12.1 Class PipedWriter 204

12.2 Class PipedReader 205

12.3 Class CharArrayWriter 206

12.4 Class CharArrayReader 208

12.5 Class StringWriter 209

12.6 Class StringReader 210

12.7 Another line-at-a-time Reader 210

12.8 Wrapping up 221

13 Object streams 213

13.1 Overview 214

13.2 Class ObjectOutputStream 216

13.3 Class ObjectInputStream 220

13.4 Object stream exceptions 224

13.5 Interface Serializable 224

13.6 Interface Externalizable 226

13.7 Interface ObjectInputValidation 226

13.8 Creating a serializable class 227

13.9 Subclassing the object streams 235

13.10 Class versioning 241

13.11 Wrapping up 244

13.12 Conclusion 244

Part III Networking

14 Client-side networking 251

14.1 Class InetAddress 252

14.2 An InetAddress example 253

14.3 Class Socket 256

14.4 Getting Web pages with a socket 263

14.5 Building a PostOutputStream class 267

14.6 Wrapping up 271

15 Some example TCP/IP clients 273

15.1 Internet protocols 274

15.2 A finger client 275

15.3 A DNS client 282

15.4 Wrapping up 306

16 Server-side networking 309

16.1 Class ServerSocket 310

16.2 Building an echo server 313

16.3 Building a nonblocking server 315

16.4 Building a multithreaded echo server 320

16.5 Wrapping up 322

17 Building a Web server 325

17.1 Resources on the Web 326

17.2 The HyperText Transfer Protocol 327

17.3 HTTP requests 328

17.4 HTTP Responses 330

17.5 The Common Gateway Interface 332

17.6 Implementing a Web server 334

17.7 Conclusion 360

18 Client/server Internet applications 361

18.1 Building a chat client 362

18.2 Building a multithreaded server 367

18.3 Networking with the object streams 371

18.4 A distributed list datastructure 373

18.5 A Socket-based distributed list 379

18.6 Wrapping up 398

19 The URL classes 401

19.1 Overview 402

19.2 Class URL 403

19.3 Using the URL class 406

19.4 Class URLConnection 407

19.5 Using the URLConnection class 413

19.6 Class HttpURL Connection 414

19.7 Interface URLStreamHandlerFactory 418

19.8 Class URLStreamHandler 419

19.9 Interface ContentHandlerFactory 420

19.10 Class ContentHandler 421

19.11 An HTTP protocol handler 422

19.12 A plaintext content handler 428

19.13 A finger protocol handler 430

19.14 Class URLEncoder 438

19.15 Class URLDecoder 438

19.16 Wrapping up 439

20 Datagram networking 441

20.1 Class DatagramPacket 442

20.2 Class DatagramSocket 443

20.3 Receiving UDP packets 447

20.4 Transmitting UDP packets 448

20.5 A UDP example 449

20.6 Wrapping up 456

21 Some datagram applications 459

21.1 A ping client 460

21.2 DNS over UDP 468

21.3 A daytime server 471

21.4 Wrapping up 473

22 Multicast networking 475

22.1 Multicast 476

22.2 Broadcast 479

22.3 Class MulticastSocket 480

22.4 Using multicast 483

22.5 A peer-to-peer multicast chat system 485

22.6 A client/server multicast chat system 490

22.7 Wrapping up 494

22.8 Conclusion 495

Part IV Alternative technologies

23 Remote method invocation 499

23.1 Introduction 501

23.2 An RMI date server 505

23.3 RMI class file locations 509

23.4 RMI-related packages 514

23.5 Interface Remote 515

23.6 Class Naming 517

23.7 Class LocateRegistry 520

23.8 Interface Registry 522

23.9 Class RemoteObject 524

23.10 Class RemoteServer 526

23.11 Class UnicastRemoteObject 527

23.12 Class RemoteStub 529

23.13 Interface Unreferenced 529

23.14 Interface RMIFailureHandler 532

23.15 Class RMISocketFactory 532

23.16 Interface RMIClientSocketFactory 533

23.17 Interface RMIServerSocketFactory 534

23.18 RMI Object Activation 534

23.19 Wrapping up 535

24 RMI in practice 535

24.1 An RMI chat system 536

24.2 An RMI chat with callback 534

24.3 An RMI distributed list 550

24.4 An RMI peering service 561

24.5 RMI and Web browsers 565

25 CORBA 570

25.1 Introduction 571

25.2 Why distribute using CORBA? 571

25.3 The CORBA core architecture 572

25.4 Interface definition language 572

25.5 A bank account example 575

25.6 The CORBA object model 577

25.7 Standard CORBA services 578

25.8 A CORBA bank account 581

25.9 A CORBA bank client 587

25.10 Conclusion 589

25.11 References 589

26 Servlets 591

26.1 Technology comparison 593

26.2 Invoking servlets 595

26.3 Developing servlets 597

26.4 Servlet internals 599

26.5 Interface Servlet 601

26.6 Interface SingleThreadModel 603

26.7 Interface ServletConfig 603

26.8 Interface ServletRequest 604

26.9 Interface ServletResponse 606

26.10 Interface ServletContext 607

26.11 Class ServletInputStream 608

26.12 Class ServletOutputStream 609

26.13 Class GenericServlet 609

26.14 A server-side include example 611

26.15 Class HttpServlet 612

26.16 Interface HttpServletRequest 615

26.17 Interface HttpServletResponse 617

26.18 Interface HttpSession 620

26.19 Interface HttpSessionBindingListener 622

26.20 Class HttpSessionBindingEvent 622

26.21 Class Cookie 623

26.22 Servicing an HTTP get request 625

26.23 Servicing an HTTP post request 627

26.24 Wrapping up 628

27 Servlets in practice 629

27.1 Persistence in servlets 630

27.2 An access-counter servlet 635

27.3 Using servlets for collaboration 638

27.4 A servlet-based distributed list 640

27.5 Conclusion 655

Part V Message streams

28 Message streams 659

28.1 Benefits of messages 660

28.2 Class MessageOutput 661

28.3 Class MessageInput 663

28.4 Class MessageOutputStream 664

28.5 Class MessageInputStream 666

28.6 Using message streams 667

28.7 Class MessageCopier 668

28.8 A transaction processing example 670

28.9 Wrapping up 676

29 Queuing message streams 677

29.1 Class Queue 678

29.2 Class QueueOutputStream 680

29.3 Class QueueInputStream 681

29.4 Filling a queue 682

29.5 Emptying a queue 683

29.6 Wrapping up 685

30 Multiplexing message streams 687

30.1 Class MultiplexOutputStream 688

30.2 Class MultiplexInputStream 690

30.3 Class Demultiplexer 692

30.4 Class DeliveryOutputStream 694

30.5 Interface Recipient 696

30.6 A simple collaborative tool 696

30.7 Wrapping up 706

31 Routing message streams 709

31.1 Class RoutingOutputStream 710

31.2 Class RoutingInputStream 712

31.3 Class Router 713

31.4 Wrapping up 717

32 A generic message server 719

32.1 Class GenericServer 720

32.2 Class GenericMessageCopier 722

32.3 Class GenericHandler 723

32.4 Wrapping up 729

33 Building a generic client 731

33.1 Class GenericClient 732

33.2 Interface Client 739

33.3 Building an advanced chat system 740

33.4 Extending the generic classes 745

33.5 Wrapping up 747

Appendices

Networking 751

Overview of networks 752

Local-area networks 754

Wide-area networking 756

The Internet 7764

References 774

Tables 775

Java escaped characters 776

Character encodings 776

Character encoding aliases 781

The ASCII character set 783

Unicode 2.0 block allocations 785

Modified UTF-8 Encoding 787

Multiplication tables 1 to 6 788

IP address classes 788

Selected well-known UNIX TCP and UDP services 789

HTTP requests 790

HTTP responses 790

HTTP 792

CGI environment variables 795

MIME types 795

Permission objects 797

index 799