Java Network Programming, 4th Edition

Book description

This practical guide provides a complete introduction to developing network programs with Java. You’ll learn how to use Java’s network class library to quickly and easily accomplish common networking tasks such as writing multithreaded servers, encrypting communications, broadcasting to the local network, and posting data to server-side programs.

Author Elliotte Rusty Harold provides complete working programs to illustrate the methods and classes he describes. This thoroughly revised fourth edition covers REST, SPDY, asynchronous I/O, and many other recent technologies.

  • Explore protocols that underlie the Internet, such as TCP/IP and UDP/IP
  • Learn how Java’s core I/O API handles network input and output
  • Discover how the InetAddress class helps Java programs interact with DNS
  • Locate, identify, and download network resources with Java’s URI and URL classes
  • Dive deep into the HTTP protocol, including REST, HTTP headers, and cookies
  • Write servers and network clients, using Java’s low-level socket classes
  • Manage many connections at the same time with the nonblocking I/O

Publisher resources

View/Submit Errata

Table of contents

  1. Dedication
  2. Preface
    1. About the Fourth Edition
    2. Organization of the Book
    3. Who You Are
    4. Java Versions
    5. About the Examples
    6. Conventions Used in This Book
    7. Request for Comments
    8. Using Code Examples
    9. Safari® Books Online
    10. How to Contact Us
    11. Acknowledgments
  3. 1. 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 Blocks
      2. Network Address Translation
      3. Firewalls
      4. Proxy Servers
    5. The Client/Server Model
    6. Internet Standards
      1. IETF RFCs
      2. W3C Recommendations
  4. 2. Streams
    1. Output Streams
    2. Input Streams
      1. Marking and Resetting
    3. Filter Streams
      1. Chaining Filters Together
      2. Buffered Streams
      3. PrintStream
      4. Data Streams
    4. Readers and Writers
      1. Writers
      2. OutputStreamWriter
      3. Readers
      4. Filter Readers and Writers
      5. PrintWriter
  5. 3. 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
      4. Futures, Callables, and Executors
    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. Finish
    6. Thread Pools and Executors
  6. 4. Internet Addresses
    1. The InetAddress Class
      1. Creating New InetAddress Objects
        1. Caching
        2. Lookups by IP address
        3. Security issues
      2. Getter Methods
      3. Address Types
      4. Testing Reachability
      5. Object Methods
    2. Inet4Address and Inet6Address
    3. The NetworkInterface Class
      1. Factory Methods
        1. public static NetworkInterface getByName(String name) throws SocketException
        2. public static NetworkInterface getByInetAddress(InetAddress address) throws SocketException
        3. public static Enumeration getNetworkInterfaces() throws SocketException
      2. Getter Methods
        1. public Enumeration getInetAddresses()
        2. public String getName()
        3. public String getDisplayName()
    4. Some Useful Programs
      1. SpamCheck
      2. Processing Web Server Logfiles
  7. 5. URLs and URIs
    1. URIs
      1. URLs
      2. Relative URLs
    2. 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. Other sources of URL objects
      2. 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
      3. Splitting a URL into Pieces
        1. public String getProtocol()
        2. public String getHost()
        3. public int getPort()
        4. public int getDefaultPort()
        5. public String getFile()
        6. public String getPath()
        7. public String getRef()
        8. public String getQuery()
        9. public String getUserInfo()
        10. public String getAuthority()
      4. Equality and Comparison
      5. Conversion
    3. The URI Class
      1. Constructing a URI
      2. The Parts of the URI
      3. Resolving Relative URIs
      4. Equality and Comparison
      5. String Representations
    4. x-www-form-urlencoded
      1. URLEncoder
      2. URLDecoder
    5. Proxies
      1. System Properties
      2. The Proxy Class
      3. The ProxySelector Class
    6. Communicating with Server-Side Programs Through GET
    7. Accessing Password-Protected Sites
      1. The Authenticator Class
      2. The PasswordAuthentication Class
      3. The JPasswordField Class
  8. 6. HTTP
    1. The Protocol
      1. Keep-Alive
    2. HTTP Methods
    3. The Request Body
    4. Cookies
      1. CookieManager
      2. CookieStore
  9. 7. URLConnections
    1. Opening URLConnections
    2. Reading Data from a Server
    3. Reading the Header
      1. Retrieving Specific 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 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. Caches
      1. Web Cache for Java
    5. Configuring the Connection
      1. protected URL url
      2. protected boolean connected
      3. protected boolean allowUserInteraction
      4. protected boolean doInput
      5. protected boolean doOutput
      6. protected boolean ifModifiedSince
      7. protected boolean useCaches
      8. Timeouts
    6. Configuring the Client Request HTTP Header
    7. Writing Data to a Server
    8. Security Considerations for URLConnections
    9. Guessing MIME Media Types
    10. HttpURLConnection
      1. The Request Method
        1. HEAD
        2. DELETE
        3. PUT
        4. OPTIONS
        5. TRACE
      2. Disconnecting from the Server
      3. Handling Server Responses
        1. Error conditions
        2. Redirects
      4. Proxies
      5. Streaming Mode
  10. 8. Sockets for Clients
    1. Using Sockets
      1. Investigating Protocols with Telnet
      2. Reading from Servers with Sockets
      3. Writing to Servers with Sockets
        1. Half-closed sockets
    2. Constructing and Connecting Sockets
      1. Basic Constructors
      2. Picking a Local Interface to Connect From
      3. Constructing Without Connecting
      4. Socket Addresses
      5. Proxy Servers
    3. Getting Information About a Socket
      1. Closed or Connected?
      2. toString()
    4. Setting Socket Options
      1. TCP_NODELAY
      2. SO_LINGER
      3. SO_TIMEOUT
      4. SO_RCVBUF and SO_SNDBUF
      5. SO_KEEPALIVE
      6. OOBINLINE
      7. SO_REUSEADDR
      8. IP_TOS Class of Service
    5. Socket Exceptions
    6. Sockets in GUI Applications
      1. Whois
      2. A Network Client Library
  11. 9. Sockets for Servers
    1. Using ServerSockets
      1. Serving Binary Data
      2. Multithreaded Servers
      3. Writing to Servers with Sockets
      4. Closing Server Sockets
    2. Logging
      1. What to Log
      2. How to Log
    3. Constructing Server Sockets
      1. Constructing Without Binding
    4. Getting Information About a Server Socket
    5. Socket Options
      1. SO_TIMEOUT
      2. SO_REUSEADDR
      3. SO_RCVBUF
      4. Class of Service
    6. HTTP Servers
      1. A Single-File Server
      2. A Redirector
      3. A Full-Fledged HTTP Server
  12. 10. Secure Sockets
    1. Secure Communications
    2. Creating Secure Client Sockets
    3. Choosing the Cipher Suites
    4. Event Handlers
    5. Session Management
    6. Client Mode
    7. Creating Secure Server Sockets
    8. Configuring SSLServerSockets
      1. Choosing the Cipher Suites
      2. Session Management
      3. Client Mode
  13. 11. Nonblocking I/O
    1. An Example Client
    2. An Example Server
    3. Buffers
      1. Creating Buffers
        1. Allocation
        2. Direct allocation
        3. Wrapping
      2. Filling and Draining
      3. Bulk Methods
      4. Data Conversion
      5. View Buffers
      6. Compacting Buffers
      7. Duplicating Buffers
      8. Slicing Buffers
      9. Marking and Resetting
      10. Object Methods
    4. Channels
      1. SocketChannel
        1. Connecting
        2. Reading
        3. Writing
        4. Closing
      2. ServerSocketChannel
        1. Creating server socket channels
        2. Accepting connections
      3. The Channels Class
      4. Asynchronous Channels (Java 7)
      5. Socket Options (Java 7)
    5. Readiness Selection
      1. The Selector Class
      2. The SelectionKey Class
  14. 12. UDP
    1. The UDP Protocol
    2. UDP Clients
    3. UDP Servers
    4. 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 SocketAddress getSocketAddress()
        4. public byte[] getData()
        5. public int getLength()
        6. public int getOffset()
      3. The setter Methods
        1. public void setData(byte[] data)
        2. public void setData(byte[] data, int offset, int length)
        3. public void setAddress(InetAddress remote)
        4. public void setPort(int port)
        5. public void setAddress(SocketAddress remote)
        6. public void setLength(int length)
    5. The DatagramSocket Class
      1. The Constructors
        1. public DatagramSocket() throws SocketException
        2. public DatagramSocket(int port) throws SocketException
        3. public DatagramSocket(int port, InetAddress interface) throws SocketException
        4. public DatagramSocket(SocketAddress interface) throws SocketException
        5. protected DatagramSocket(DatagramSocketImpl impl) 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()
        5. public InetAddress getLocalAddress()
        6. public SocketAddress getLocalSocketAddress()
      3. Managing Connections
        1. public void connect(InetAddress host, int port)
        2. public void disconnect()
        3. public int getPort()
        4. public InetAddress getInetAddress()
        5. public InetAddress getRemoteSocketAddress()
    6. Socket Options
      1. SO_TIMEOUT
      2. SO_RCVBUF
      3. SO_SNDBUF
      4. SO_REUSEADDR
      5. SO_BROADCAST
      6. IP_TOS
    7. Some Useful Applications
      1. Simple UDP Clients
      2. UDPServer
      3. A UDP Echo Client
    8. DatagramChannel
      1. Using DatagramChannel
        1. Opening a socket
        2. Receiving
        3. Sending
        4. Connecting
        5. Reading
        6. Writing
        7. Closing
        8. Socket Options // Java 7
  15. 13. IP Multicast
    1. Multicasting
      1. Multicast Addresses and Groups
      2. Clients and Servers
      3. Routers and Routing
    2. Working with Multicast Sockets
      1. The Constructors
      2. Communicating with a Multicast Group
        1. Joining groups
        2. Leaving groups and closing the connection
        3. Sending multicast data
        4. Loopback mode
        5. Network interfaces
    3. Two Simple Examples
  16. Index
  17. Colophon
  18. Copyright

Product information

  • Title: Java Network Programming, 4th Edition
  • Author(s): Elliotte Rusty Harold
  • Release date: October 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449365950