You are previewing Learning Network Programming with Java.
O'Reilly logo
Learning Network Programming with Java

Book Description

Harness the hidden power of Java to build network-enabled applications with lower network traffic and faster processes

About This Book

  • Learn to deliver superior server-to-server communication through the networking channels
  • Gain expertise of the networking features of your own applications to support various network architectures such as client/server and peer-to-peer
  • Explore the issues that impact scalability, affect security, and allow applications to work in a heterogeneous environment

Who This Book Is For

Learning Network Programming with Java is oriented to developers who wish to use network technologies to enhance the utility of their applications. You should have a working knowledge of Java and an interest in learning the latest in network programming techniques using Java. No prior experience with network development or special software beyond the Java SDK is needed. Upon completion of the book, beginner and experienced developers will be able to use Java to access resources across a network and the Internet.

What You Will Learn

  • Connect to other applications using sockets
  • Use channels and buffers to enhance communication between applications
  • Access network services and develop client/server applications
  • Explore the critical elements of peer-to-peer applications and current technologies available
  • Use UDP to perform multicasting
  • Address scalability through the use of core and advanced threading techniques
  • Incorporate techniques into an application to make it more secure
  • Configure and address interoperability issues to enable your applications to work in a heterogeneous environment

In Detail

Network-aware applications are becoming more prevalent and play an ever-increasing role in the world today. Connecting and using an Internet-based service is a frequent requirement for many applications. Java provides numerous classes that have evolved over the years to meet evolving network needs. These range from low-level socket and IP-based approaches to those encapsulated in software services.

This book explores how Java supports networks, starting with the basics and then advancing to more complex topics. An overview of each relevant network technology is presented followed by detailed examples of how to use Java to support these technologies.

We start with the basics of networking and then explore how Java supports the development of client/server and peer-to-peer applications. The NIO packages are examined as well as multitasking and how network applications can address practical issues such as security.

A discussion on networking concepts will put many network issues into perspective and let you focus on the appropriate technology for the problem at hand. The examples used will provide a good starting point to develop similar capabilities for many of your network needs.

Style and approach

Each network technology’s terms and concepts are introduced first. This is followed up with code examples to explain these technologies. Many of the examples are supplemented with alternate Java 8 solutions when appropriate. Knowledge of Java 8 is not necessary but these examples will help you better understand the power of Java 8.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

Table of Contents

  1. Learning Network Programming with Java
    1. Table of Contents
    2. Learning Network Programming with Java
    3. Credits
    4. About the Author
    5. About the Reviewer
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started with Network Programming
      1. Network addressing using the InetAddress class
      2. NIO support
        1. Using the URLConnection class
        2. Using the URLConnection class with buffers and channels
      3. The client/server architecture
      4. Creating a simple echo server
        1. Creating a simple echo client
        2. Using Java 8 to support the echo server and client
      5. UDP and multicasting
        1. Creating a multicast server
        2. Creating the multicast client
      6. Scalability
        1. Creating a threaded server
        2. Using the threaded server
      7. Security
        1. Creating a SSL server
        2. Creating an SSL client
        3. Generating secure keys
      8. Summary
    9. 2. Network Addressing
      1. Networking basics
        1. Understanding network basics
          1. Network architectures and protocols
      2. Using the NetworkInterface class
        1. Getting a MAC address
          1. Getting a specific MAC address
          2. Getting multiple MAC addresses
      3. Network addressing concepts
        1. URL/URI/URN
        2. Using the URI class
          1. Creating URI instances
          2. Splitting apart a URI
        3. Using the URL class
          1. Creating URL instances
          2. Splitting apart a URL
        4. IP addresses and the InetAddress class
          1. Obtaining information about an address
          2. Address scoping issues
          3. Testing reachability
        5. Introducing the Inet4Address
          1. Private addresses in IPv4
          2. IPv4 address types
          3. The Inet4Address class
          4. Special IPv4 addresses
        6. Introducing the Inet6Address class
          1. Private addresses in IPv6
          2. The Inet6Address class
          3. Special IPv6 addresses
        7. Testing for the IP address type
          1. Using IPv4-compatible IPv6 addresses
      4. Controlling network properties
      5. Summary
    10. 3. NIO Support for Networking
      1. Java NIO
      2. Introduction to buffers
      3. Using channels with a time server
        1. Creating a time server
        2. Creating a time client
      4. The chat server/client applications
        1. The chat server
        2. The chat client
        3. Server/client interaction
        4. The HelperMethods class
          1. Handling variable length messages
        5. Running the chat server/client application
      5. Handling multiple clients
        1. The parts server
        2. The parts client handler
        3. The parts client
        4. Running the parts client/server
      6. Asynchronous socket channels
        1. Creating the asynchronous server socket channel server
        2. Creating the asynchronous socket channel client
      7. Other buffer operations
        1. Bulk data transfer
        2. Using a view
        3. Using read-only buffers
      8. Controlling socket options
      9. Summary
    11. 4. Client/Server Development
      1. The HTTP protocol structure
      2. The nature of HTTP messages
        1. Initial request line format
        2. Header lines
        3. Message body
        4. Client/Server interaction example
      3. Java socket support for HTTP client/server applications
        1. Building a simple HTTP server
        2. Building a simple HTTP client
      4. Client/server development using standard Java classes
        1. Using the HttpURLConnection class
          1. URL encoding
        2. Using the HTTPServer class
          1. Implementing a simple HTTPServer class
          2. Managing response headers
      5. Open source Java HTTP servers
      6. Server configuration
      7. Handling cookies
      8. Summary
    12. 5. Peer-to-Peer Networks
      1. P2P functions/characteristics
      2. Applications-based P2P networks
      3. Java support for P2P applications
      4. Distributed hash tables
        1. DHT components
        2. DHT implementations
        3. Using JDHT
      5. Using FreePastry
        1. The FreePastry demonstration
          1. Understanding the FreePastryExample class
          2. Understanding the FreePastryApplication class
          3. Sending a message to a specific node
      6. Summary
    13. 6. UDP and Multicasting
      1. Java support for UDP
      2. TCP versus UDP
      3. UDP client/server
        1. The UDP server application
        2. The UDP client application
        3. The UDP client/server in action
      4. Channel support for UDP
        1. The UDP echo server application
        2. The UDP echo client application
        3. The UDP echo client/server in action
      5. UDP multicasting
        1. The UDP multicast server
        2. The UDP multicast client
        3. The UDP multicast client/server in action
      6. UDP multicasting with channels
        1. The UDP channel multicast server
        2. The UDP channel multicast client
        3. The UDP channel multicast client/server in action
      7. UDP streaming
        1. The UDP audio server implementation
        2. The UDP audio client implementation
      8. Summary
    14. 7. Network Scalability
      1. Multithreaded server overview
      2. The thread-per-request approach
        1. The thread-per-request server
        2. The thread-per-request client
        3. The thread-per-request applications in action
      3. Thread-per-connection approach
        1. The thread-per-connection server
        2. The thread-per-connection client
        3. The thread-per-connection applications in action
      4. Thread pools
        1. The ThreadPoolExecutor class characteristics
        2. Simple thread pool server
        3. Simple thread pool client
        4. The thread pool client/server in action
        5. Thread pool with Callable
          1. Using a Callable
          2. Using a Future
      5. Using the HttpServer executor
      6. Using a selector
        1. Creating the selector
        2. Registering a channel
        3. Using the selector to support a time client/server
          1. The channel time server
          2. The date and time client application
          3. The date and time server/client in action
      7. Handling network timeouts
      8. Summary
    15. 8. Network Security
      1. Security
        1. Secure communication terminology
      2. Encryption basics
        1. Symmetric encryption techniques
          1. Generating a key
          2. Encrypting text using a symmetric key
          3. Decrypting text
        2. Asymmetric encryption techniques
          1. Generating and saving asymmetric keys
          2. Encrypting/decrypting text using an asymmetric key
          3. Saving asymmetric keys to a file
      3. Creating a keystore
        1. Creating and maintaining a keystore with keytool
        2. Keytool command-line arguments
        3. Creating and maintaining a keystore with Java
      4. Symmetric encryption client/server
        1. Symmetric server application
        2. Symmetric client application
        3. Symmetric client/server in action
      5. Asymmetric encryption client/server
        1. Asymmetric server application
        2. Asymmetric client application
        3. Asymmetric client/server in action
      6. TLS/SSL
        1. SSL server
        2. SSL client
        3. SSL client/server in action
      7. Secure hash functions
      8. Summary
    16. 9. Network Interoperability
      1. Byte order in Java
      2. Interfacing with other languages
        1. Interfacing with JVM based languages
        2. Interfacing with non-JVM languages
      3. Communication through simple sockets
        1. The Java server
        2. The C# client
        3. The client/server in action
      4. Interoperability through middleware
        1. Creating a RESTful service
        2. Testing the RESTful service
        3. Creating a RESTful client
      5. Summary
    17. Index