You are previewing Java Network Programming, Second Edition.
O'Reilly logo
Java Network Programming, Second Edition

Book Description

Java Network Programming, 2nd Edition, is a complete introduction to developing network programs (both applets and applications) using Java, covering everything from Networking fundamentals to remote method invocation (RMI). It includes chapters on TCP and UDP sockets, multicasting protocol and content handlers, and servlets. This second edition also includes coverage of Java 1.1, 1.2 and 1.3. New chapters cover multithreaded network programming, I/O, HTML parsing and display, the Java Mail API, the Java Secure Sockets Extension, and more.

Table of Contents

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