Home Page Icon
Home Page
Table of Contents for
Cover
Close
Cover
by Elliotte Rusty Harold
Java Network Programming, Second Edition
Java Network Programming, 2nd Edition
Preface
About the Second Edition
Organization of the Book
Who You Are
Java Versions
Security
About the Examples
Conventions Used in This Book
Request for Comments
Acknowledgments
1. Why Networked Java?
What Can a Network Program Do?
Retrieve Data and Display It
Repeatedly Retrieve Data
Send Data
File storage
Massively parallel computing
Smart forms
Peer-to-Peer Interaction
Games
Chat
Whiteboards
Servers
Searching the Web
Electronic Commerce
Applications of the Future
Ubiquitous computing
Interactive television
Collaboration
But Wait!—There’s More!
2. Basic Network Concepts
Networks
The Layers of a Network
The Host-to-Network Layer
The Internet Layer
The Transport Layer
The Application Layer
IP, TCP, and UDP
IP Addresses and Domain Names
Ports
The Internet
Internet Address Classes
Firewalls
Proxy Servers
The Client/Server Model
Internet Standards
IETF RFCs
W3C Recommendations
3. Basic Web Concepts
URIs
URNs
URLs
Relative URLs
HTML, SGML, and XML
HTTP
MIME
CGI
Applets and Security
Where Do Applets and Classes Come from?
Security: Who Can an Applet Talk to and What Can It Say?
4. Java I/O
Output Streams
Input Streams
Marking and Resetting
Filter Streams
Chaining Filters Together
Buffered Streams
PrintStream
PrintStream is evil and network programmers shouldavoid it like the plague
PushbackInputStream
Data Streams
Compressing Streams
Digest Streams
Encrypting Streams
Readers and Writers
Writers
OutputStreamWriter
Readers
Filter Readers and Writers
Buffered readers and writers
LineNumberReader
PushbackReader
PrintWriter
5. Threads
Running Threads
Subclassing Thread
Implementing the Runnable Interface
Returning Information from a Thread
Race Conditions
Polling
Callbacks
Synchronization
Synchronized Blocks
Synchronized Methods
Alternatives to Synchronization
Deadlock
Thread Scheduling
Priorities
Preemption
Blocking
Yielding
Sleeping
Joining threads
Waiting on an object
Priority-based preemption
Finish
Thread Pools
6. Looking Up Internet Addresses
DNS, IP Addresses, and All That
The InetAddress Class
Creating New InetAddress Objects
public static InetAddress InetAddress.getByName(String hostName) throws UnknownHostException
public static InetAddress[ ] InetAddress.getAllByName (String hostName) throws UnknownHostException
public static InetAddress InetAddress.getLocalHost( ) throws UnknownHostException
Security issues
Other sources of InetAddress objects
Getter Methods
public String getHostName( )
public String getHostAddress( )
public byte[ ] getAddress( )
Object Methods
public boolean equals(Object o)
public int hashCode( )
public String toString( )
Some Useful Programs
HostLookup
Processing Web Server Log Files
7. Retrieving Data with URLs
The URL Class
Creating New URLs
Constructing a URL from a string
Constructing a URL from its component parts
Constructing relative URLs
Specifying a URLStreamHandler
Other sources of URL objects
Splitting a URL into Pieces
public String getProtocol( )
public String getHost( )
public int getPort( )
public String getFile( )
public String getPath( ) // Java 1.3
public string getRef( )
public string getQuery( ) // Java 1.3
public string getUserInfo( ) // Java 1.3
public string getAuthority( ) // Java 1.3
Retrieving Data from a URL
public final InputStream openStream( ) throws IOException
public URLConnection openConnection( ) throws IOException
public final Object getContent( ) throws IOException
public final Object getContent(Class[ ] classes) throws IOException // Java 1.3
Utility Methods
public boolean sameFile(URL other)
public String toExternalForm( )
The Object Methods
public String toString( )
public boolean equals(Object o)
public int hashCode( )
Methods for Protocol Handlers
public static synchronized void setURLStreamHandlerFactory (URLStreamHandlerFactory factory)
protected void set(String protocol, String host, int port, String file, String ref )
The URLEncoder and URLDecoder Classes
URLEncoder
URLDecoder
Communicating with CGIs and Servlets Through GET
Accessing Password-Protected Sites
The Authenticator Class
The PasswordAuthentication Class
The JPasswordField Class
8. HTML in Swing
HTML on Components
JEditorPane
Constructing HTML User Interfaces on the Fly
Handling Hyperlinks
Reading HTML Directly
Parsing HTML
HTMLEditorKit.Parser
HTMLEditorKit.ParserCallback
HTML.Tag
Attributes
9. The Network Methods of java.applet.Applet
Using java.applet.Applet to Download Data
Figuring Out Where the Applet Came from
public URL getDocumentBase( )
public URL getCodeBase( )
Downloading Images
public Image getImage(URL u)
public Image getImage(URL path, String filename)
Downloading Sounds
public void play(URL u)
public void play(URL url, String filename)
public AudioClip getAudioClip(URL u)
public AudioClip getAudioClip(URL url, String filename)
public static final AudioClip newAudioClip(URL url) // Java 1.2
The ImageObserver Interface
The ImageObserver Constants
The MediaTracker Class
The Constructor
Adding Images to MediaTrackers
public void addImage(Image image, int id)
public void addImage(Image image, int id, int width, int height)
Checking Whether Media Has Loaded
public boolean checkID(int id)
public boolean checkID(int id, boolean load)
public boolean checkAll( )
public boolean checkAll(boolean load)
Waiting for Media to Load
public void waitForID(int id) throws InterruptedException
public boolean waitForID(int id, long milliseconds) throws InterruptedException
public boolean waitForAll( ) throws InterruptedException
public boolean waitForAll(long milliseconds) throws InterruptedException
Error Checking
public boolean isErrorAny( )
public Object[ ] getErrorsAny( )
public boolean isErrorID(int id)
public Object[ ] getErrorsID(int id)
Checking the Status of Media
public int statusAll(boolean load)
public int statusID(int id, boolean load)
Removing Images from MediaTrackers
Network Methods of java.applet.AppletContext
10. Sockets for Clients
Socket Basics
Investigating Protocols with Telnet
The Socket Class
The Constructors
public Socket(String host, int port) throws UnknownHostException, IOException
public Socket(InetAddress host, int port) throws IOException
public Socket(String host, int port, InetAddress interface, int localPort) throws IOException
public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException
protected Socket( )
protected Socket(SocketImpl impl)
Getting Information About a Socket
public InetAddress getInetAddress( )
public int getPort( )
public int getLocalPort( )
public InetAddress getLocalAddress( )
public InputStream getInputStream( ) throws IOException
public OutputStream getOutputStream( ) throws IOException
Closing the Socket
public synchronized void close( ) throws IOException
Half-closed sockets // Java 1.3
The Object Methods
public String toString( )
Setting Socket Options
TCP_NODELAY
SO_LINGER
SO_TIMEOUT
SO_RCVBUF
SO_SNDBUF
SO_KEEPALIVE
Socket Exceptions
Examples
Finger
Whois
11. Sockets for Servers
The ServerSocket Class
The Constructors
public ServerSocket(int port) throws IOException, BindException
public ServerSocket(int port, int queueLength) throws IOException, BindException
public ServerSocket(int port, int queueLength, InetAddress bindAddress) throws BindException, IOException
Accepting and Closing Connections
public Socket accept( ) throws IOException
public void close( ) throws IOException
The get Methods
public InetAddress getInetAddress( )
public int getLocalPort( )
Socket Options
public void setSoTimeout(int timeout) throws SocketException
public int getSoTimeout( ) throws IOException
The Object Methods
public String toString( )
Implementation
public static synchronized void setSocketFactory (SocketImpl Factory fac) throws IOException
Protected final void implAccept(Socket s) throws IOException
Some Useful Servers
Client Tester
HTTP Servers
A single-file server
A redirector
A full-fledged HTTP server
12. Secure Sockets
Secure Communications
Creating Secure Client Sockets
Methods of the SSLSocket Class
Choosing the Cipher Suites
Event Handlers
Session Management
Client Mode
Creating Secure Server Sockets
Methods of the SSLServerSocket Class
Choosing the Cipher Suites
Session Management
Client Mode
13. UDP Datagrams and Sockets
The UDP Protocol
The DatagramPacket Class
The Constructors
Constructors for receiving datagrams
Constructors for sending datagrams
The get Methods
public InetAddress getAddress( )
public int getPort( )
public byte[ ] getData( )
public int getLength( )
public int getOffset( ) // Java 1.2
The set Methods
public void setData(byte[ ] data)
public void setData(byte[ ] data, int offset, int length) // Java 1.2
public void setAddress(InetAddress remote)
public void setPort(int port)
public void setLength(int length)
The DatagramSocket Class
The Constructors
public DatagramSocket( ) throws SocketException
public DatagramSocket(int port) throws SocketException
public DatagramSocket(int port, InetAddress address) throws SocketException
Sending and Receiving Datagrams
public void send(DatagramPacket dp) throws IOException
public void receive(DatagramPacket dp) throws IOException
public void close( )
public int getLocalPort( )
Managing Connections
public void connect(InetAddress host, int port) // Java 1.2
public void disconnect( ) // Java 1.2
public int getPort( ) // Java 1.2
public InetAddress getInetAddress( ) // Java 1.2
Socket Options
SO_TIMEOUT
SO_RCVBUF
SO_SNDBUF
Some Useful Applications
Simple UDP Clients
UDPServer
A UDP Echo Client
14. Multicast Sockets
What Is a Multicast Socket?
Multicast Addresses and Groups
Clients and Servers
Routers and Routing
Working with Multicast Sockets
The Constructors
public MulticastSocket( ) throws SocketException
public MulticastSocket(int port) throws SocketException
Communicating with a Multicast Group
public void joinGroup(InetAddress address) throws IOException
public void leaveGroup(InetAddress address) throws IOException
public void send(DatagramPacket packet, byte ttl) throws IOException
public void setInterface(InetAddress address) throws SocketException
public InetAddress getInterface( ) throws SocketException
public void setTimeToLive(int ttl) throws IOException // Java 1.2
public int getTimeToLive( ) throws IOException // Java 1.2
Two Simple Examples
15. The URLConnection Class
Opening URLConnections
Reading Data from a Server
Reading the Header
Retrieving Specific MIME Header Fields
public String getContentType( )
public int getContentLength( )
public String getContentEncoding( )
public long getDate( )
public long getExpiration( )
public long getLastModified( )
Retrieving Arbitrary MIME Header Fields
public String getHeaderField(String name)
public String getHeaderFieldKey(int n)
public String getHeaderField(int n)
public long getHeaderFieldDate(String name, long default)
public int getHeaderFieldInt(String name, int default)
Configuring the Connection
protected URL url
connected
allowUserInteraction
defaultAllowUserInteraction
doInput
doOutput
ifModifiedSince
useCaches
defaultUseCaches
Configuring the Client Request MIME Header
Writing Data to a Server
Content Handlers
Getting Content
public Object getContent( ) throws IOException
public Object getContent(Class[ ] classes) throws IOException // Java 1.3
ContentHandlerFactory
The Object Methods
Security Considerations for URLConnections
Guessing MIME Types
HttpURLConnection
The Request Method
HEAD
OPTIONS
DELETE
PUT
TRACE
Disconnecting from the Server
Handling Server Responses
Error conditions
Redirects
Proxies
JarURLConnection
16. Protocol Handlers
What Is a Protocol Handler?
The URLStreamHandler Class
The Constructor
Methods for Parsing URLs
Protected void parseURL(URL u, String spec, int start, int limit)
Protected String toExternalForm(URL u)
Protected void setURL(URL u, String protocol, String host, int port, String file, String ref )
Protected int getDefaultPort( ) // Java 1.3
Protected InetAddress getHostAddress(URL u) // Java 1.3
Protected boolean hostsEqual(URL u1, URL u2) // Java 1.3
Protected boolean sameFile(URL u1, URL u2) // Java 1.3
Protected boolean equals(URL u1, URL u2) // Java 1.3
Protected int hashCode(URL u) // Java 1.3
A Method for Connecting
Protected abstract URLConnection openConnection(URL u) throws IOException
Writing a Protocol Handler
More Protocol Handler Examples and Techniques
A daytime Protocol Handler
A chargen Protocol Handler
The URLStreamHandlerFactory Interface
17. Content Handlers
What Is a Content Handler?
The ContentHandler Class
A Content Handler for Tab-Separated Values
Using Content Handlers
Choosing Return Types
The ContentHandlerFactory Interface
The createContentHandler( ) Method
Installing Content Handler Factories
A Content Handler for an Image Format: image/x-fits
18. Remote Method Invocation
What Is Remote Method Invocation?
Security
Object Serialization
Under the Hood
Implementation
The Server Side
The Client Side
Compiling the Stubs
Starting the Server
Running the Client
Loading Classes at Runtime
An Applet Client for a Remote Object
An Application Client for a Remote Object
The java.rmi Package
The Remote Interface
The Naming Class
public static String[ ] list(String url) throws RemoteException, MalformedURLException
public static Remote lookup(String url) throws RemoteException, NotBoundException, AccessException, MalformedURLException
public static void bind(String url, Remote object) throws RemoteException, AlreadyBoundException, MalformedURLException, AccessException
public static void unbind(String url) throws RemoteException, NotBoundException, AlreadyBoundException, MalformedURLException, AccessException // Java 1.2
public static void rebind(String url, Remote object) throws RemoteException, AccessException, MalformedURLException
The RMISecurityManager Class
public RMISecurityManager( )
public Object getSecurityContext( )
Checking operations
Remote Exceptions
The java.rmi.registry Package
The Registry Interface
The LocateRegistry Class
The java.rmi.server Package
The RemoteObject Class
The RemoteServer Class
Constructors
Getting information about the client
Logging
The UnicastRemoteObject Class
Exceptions
19. The JavaMail API
What Is the JavaMail API?
Sending Email
Sending Email from an Application
Sending Email from an Applet
Receiving Mail
Password Authentication
Addresses
The Address Class
The InternetAddress Class
The NewsAddress Class
The URLName Class
The Constructors
Parsing Methods
The Message Class
Creating Messages
Replying to messages
Getting messages from folders
Basic Header Info
The From address
The Reply-to address
The recipient addresses
The subject of the message
The date of the message
Saving changes
Flags
Folders
Searching
The Part Interface
Attributes
Headers
Content
Reading the contents of the part
Writing the contents of the part
Multipart Messages and File Attachments
MIME Messages
Folders
Opening Folders
Basic Folder Info
Managing Folders
Managing Messages in Folders
Subscriptions
Listing the Contents of a Folder
Checking for Mail
Getting Messages from Folders
Searching Folders
Flags
Event Handling
Utility Methods
Index
Colophon
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
Java Network Programming, 2nd Edition
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset