Table of
Contents
Java™ Network Programming and Distributed Computing
By David Reilly
, Michael Reilly
Publisher : Addison Wesley
Pub Date : March 25, 2002
ISBN : 0-201-71037-4
Pages : 496
Java(TM) Network Programming and Distributed Computing is an accessible
introduction to the changing face of networking theory, Java(TM) technology, and the
fundamental elements of the Java networking API. With the explosive growth of the
Internet, Web applications, and Web services, the majority of today's programs and
applications require some form of networking. Because it was created with extensive
networking features, the Java programming language is uniquely suited for network
programming and distributed computing.
Whether you are a Java devotee who needs a solid working knowledge of network
programming or a network programmer needing to apply your existing skills to Java, this
how-to guide is the one book you will want to keep close at hand. You will learn the
basic concepts involved with networking and the practical application of the skills
necessary to be an effective Java network programmer. An accelerated guide to
networking API, Java(TM) Network Programming and Distributed Computing also
serves as a comprehensive, example-rich reference.
You will learn to maximize the API structure through in-depth coverage of:
• The architecture of the Internet and TCP/IP
• Java's input/output system
• How to write to clients and servers using the User Datagram Protocol (UDP) and
TCP
• The advantages of multi-threaded applications
• How to implement network protocols and see examples of client/server
implementations
• HTTP and how to write server-side Java applications for the WebDistributed
computing technologies such as Remote Method Invocation (RMI) and CORBA
• How to access e-mail using the extensive and powerful JavaMail(TM) API
This book's coverage of advanced topics such as input/output streaming and multi-
threading allows even the most experienced Java developers to sharpen their skills.
Java(TM) Network Programming and Distributed Computing will get you up-to-speed
with network programming today; helping you employ innovative techniques in your
own software development projects.
B
B
B
r
r
r
o
o
o
u
u
u
g
g
g
h
h
h
t
t
t
t
t
t
o
o
o
y
y
y
o
o
o
u
u
u
b
b
b
y
y
y
o
o
o
w
w
w
n
n
n
S
S
S
k
k
k
y
y
y
!
!
!
ii
Table of Content
Table of Content................................................................................................................................i
Copyright ........................................................................................................................................... v
Dedication....................................................................................................................................vi
PREFACE ........................................................................................................................................vi
What You'll Learn .......................................................................................................................vi
What You'll Need .......................................................................................................................vii
Companion Web Site ................................................................................................................vii
Contacting the Authors .............................................................................................................vii
ACKNOWLEDGMENTS ..............................................................................................................viii
Chapter 1. Networking Theory.......................................................................................................1
1.1 What Is a Network? ..............................................................................................................1
1.2 How Do Networks Communicate? .....................................................................................2
1.3 Communication across Layers ...........................................................................................3
1.4 Advantages of Layering .......................................................................................................6
1.5 Internet Architecture ............................................................................................................. 6
1.6 Internet Application Protocols ........................................................................................... 13
1.7 TCP/IP Protocol Suite Layers ........................................................................................... 15
1.8 Security Issues: Firewalls and Proxy Servers ................................................................ 16
1.9 Summary..............................................................................................................................18
Chapter 2. Java Overview ............................................................................................................20
2.1 What Is Java?......................................................................................................................20
2.2 The Java Programming Language...................................................................................20
2.3 The Java Platform...............................................................................................................25
2.4 The Java Application Program Interface ......................................................................... 27
2.5 Java Networking Considerations...................................................................................... 28
2.6 Applications of Java Network Programming...................................................................29
2.7 Java Language Issues .......................................................................................................32
2.8 System Properties...............................................................................................................36
2.9 Development Tools.............................................................................................................37
2.10 Summary............................................................................................................................39
Chapter 3. Internet Addressing....................................................................................................40
3.1 Local Area Network Addresses ........................................................................................40
3.2 Internet Protocol Addresses..............................................................................................40
3.3 Beyond IP Addresses: The Domain Name System....................................................... 43
3.4 Internet Addressing with Java...........................................................................................46
3.5 Summary.............................................................................................................................. 49
Chapter 4. Data Streams ..............................................................................................................50
4.1 Overview .............................................................................................................................. 50
4.2 How Streams Work.............................................................................................................51
4.3 Filter Streams ......................................................................................................................60
4.4 Readers and Writers........................................................................................................... 66
4.5 Object Persistence and Object Serialization .................................................................. 79
4.6 Summary.............................................................................................................................. 88
Chapter 5. User Datagram Protocol............................................................................................ 89
5.1 Overview .............................................................................................................................. 89
5.2 DatagramPacket Class ...................................................................................................... 91
5.3 DatagramSocket Class ...................................................................................................... 93
5.4 Listening for UDP Packets................................................................................................. 95
5.5 Sending UDP packets ........................................................................................................96
5.6 User Datagram Protocol Example....................................................................................97
5.7 Building a UDP Client/Server.......................................................................................... 102
5.8 Additional Information on UDP .......................................................................................107
iii
5.9 Summary............................................................................................................................ 108
Chapter 6. Transmission Control Protocol............................................................................... 110
6.1 Overview ............................................................................................................................ 110
6.2 TCP and the Client/Server Paradigm ............................................................................ 113
6.3 TCP Sockets and Java..................................................................................................... 114
6.4 Socket Class......................................................................................................................115
6.5 Creating a TCP Client ...................................................................................................... 122
6.6 ServerSocket Class .......................................................................................................... 123
6.7 Creating a TCP Server.....................................................................................................126
6.8 Exception Handling: Socket-Specific Exceptions ........................................................ 128
6.9 Summary............................................................................................................................ 129
Chapter 7. Multi-threaded Applications .................................................................................... 130
7.1 Overview ............................................................................................................................ 130
7.2 Multi-threading in Java .....................................................................................................133
7.3 Synchronization.................................................................................................................141
7.4 Interthread Communication............................................................................................. 146
7.5 Thread Groups .................................................................................................................. 150
7.6 Thread Priorities................................................................................................................ 155
7.7 Summary............................................................................................................................ 156
Chapter 8. Implementing Application Protocols ......................................................................158
8.1 Overview ............................................................................................................................ 158
8.2 Application Protocol Specifications ................................................................................158
8.3 Application Protocol Implementation..............................................................................159
8.4 Summary............................................................................................................................ 183
Chapter 9. HyperText Transfer Protocol .................................................................................. 184
9.1 Overview ............................................................................................................................ 184
9.2 HTTP and Java .................................................................................................................192
9.3 Common Gateway Interface (CGI)................................................................................. 215
9.4 Summary............................................................................................................................ 222
Chapter 10. Java Servlets ..........................................................................................................223
10.1 Overview ..........................................................................................................................223
10.2 How Servlets Work......................................................................................................... 223
10.3 Using Servlets ................................................................................................................. 224
10.4 Running Servlets............................................................................................................. 227
10.5 Writing a Simple Servlet ................................................................................................230
10.6 SingleThreadModel ........................................................................................................ 232
10.7 ServletRequest and HttpServletRequest ....................................................................233
10.8 ServletResponse and HttpResponse .......................................................................... 235
10.9 ServletConfig................................................................................................................... 237
10.10 ServletContext...............................................................................................................238
10.11 Servlet Exceptions........................................................................................................ 239
10.12 Cookies .......................................................................................................................... 240
10.13 HTTP Session Management in Servlets................................................................... 243
10.14 Summary........................................................................................................................ 244
Chapter 11. Remote Method Invocation (RMI) ....................................................................... 246
11.1 Overview ..........................................................................................................................246
11.2 How Does Remote Method Invocation Work? ...........................................................248
11.3 Defining an RMI Service Interface ...............................................................................250
11.4 Implementing an RMI Service Interface ...................................................................... 251
11.5 Creating Stub and Skeleton Classes........................................................................... 253
11.6 Creating an RMI Server .................................................................................................253
11.7 Creating an RMI Client................................................................................................... 255
11.8 Running the RMI System...............................................................................................257
11.9 Remote Method Invocation Packages and Classes.................................................. 258
iv
11.10 Remote Method Invocation Deployment Issues...................................................... 273
11.11 Using Remote Method Invocation to Implement Callbacks ................................... 278
11.12 Remote Object Activation............................................................................................ 286
11.13 Summary........................................................................................................................ 295
Chapter 12. Java IDL and CORBA ...........................................................................................296
12.1 Overview ..........................................................................................................................296
12.2 Architectural View of CORBA .......................................................................................297
12.3 Interface Definition Language (IDL).............................................................................299
12.4 From IDL to Java ............................................................................................................302
12.5 Summary..........................................................................................................................310
Chapter 13. JavaMail ..................................................................................................................311
13.1 Overview ..........................................................................................................................311
13.2 Installing the JavaMail API ............................................................................................312
13.3 Testing the JavaMail Installation ..................................................................................313
13.4 Working with the JavaMail API ..................................................................................... 315
13.5 Advanced Messaging with JavaMail............................................................................333
13.6 Summary..........................................................................................................................342