You are previewing TCP/IP Sockets in Java.
O'Reilly logo
TCP/IP Sockets in Java

Book Description

The networking capabilities of the Java platform have been extended considerably since the first edition of the book. This new edition covers version 1.5-1.7, the most current iterations, as well as making the following improvements:

The API (application programming interface) reference sections in each chapter, which describe the relevant parts of each class, have been replaced with (i) a summary section that lists the classes and methods used in the code, and (ii) a ?gotchas? section that mentions nonobvious or poorly-documented aspects of the objects.

In addition, the book covers several new classes and capabilities introduced in the last few revisions of the Java platform. New abstractions to be covered include NetworkInterface, InterfaceAddress, Inet4/6Address, SocketAddress/InetSocketAddress, Executor, and others; extended access to low-level network information; support for IPv6; more complete access to socket options; and scalable I/O. The example code is also modified to take advantage of new language features such as annotations, enumerations, as well as generics and implicit iterators where appropriate.

Most Internet applications use sockets to implement network communication protocols. This book's focused, tutorial-based approach helps the reader master the tasks and techniques essential to virtually all client-server projects using sockets in Java. Chapter 1 provides a genral overview of networking concepts to allow readers to synchronize the concepts with terminology. Chapter 2 introduces the mechanics of simple clients and servers. Chapter 3 covers basic message construction and parsing. Chapter 4 then deals with techniques used to build more robust clients and servers. Chapter 5 (NEW) introduces the scalable interface facilities which were introduced in Java 1.5, including the buffer and channel abstractions. Chapter 6 discusses the relationship between the programming constructs and the underlying protocol implementations in more detail. Programming concepts are introduced through simple program examples accompanied by line-by-line code commentary that describes the purpose of every part of the program. The book's Web site contains many examples of command-based sockets-related code discussed throughout the book. No other resource presents so concisely or so effectively the material necessary to get up and running with Java sockets programming.

KEY FEATURES

* Focused, tutorial-based instruction in key sockets programming techniques allows reader to quickly come up to speed on Java applications.
* Concise and up-to-date coverage of the most recent platform (1.7) for Java
applications in networking technology
* Provides code for all example programs via a companion Web site to let the reader see the important objects and methods in context and to understand the purpose of each line of code.

Table of Contents

  1. Copyright
    1. Dedication
  2. The Morgan Kaufmann Practical Guides Series
  3. Preface
    1. Intended Audience
    2. Approach
    3. What This Book Is Not
    4. Acknowledgments
    5. Feedback
  4. 1. Introduction
    1. 1.1. Networks, Packets, and Protocols
    2. 1.2. About Addresses
    3. 1.3. About Names
    4. 1.4. Clients and Servers
    5. 1.5. What Is a Socket?
    6. 1.6. Exercises
  5. 2. Basic Sockets
    1. 2.1. Socket Addresses
    2. 2.2. TCP Sockets
      1. 2.2.1. TCP Client
      2. 2.2.2. TCP Server
      3. 2.2.3. Input and Output Streams
    3. 2.3. UDP Sockets
      1. 2.3.1. DatagramPacket
      2. 2.3.2. UDP Client
      3. 2.3.3. UDP Server
      4. 2.3.4. Sending and Receiving with UDP Sockets
    4. 2.4. Exercises
  6. 3. Sending and Receiving Data
    1. 3.1. Encoding Information
      1. 3.1.1. Primitive Integers
      2. 3.1.2. Strings and Text
      3. 3.1.3. Bit-Diddling: Encoding Booleans
    2. 3.2. Composing I/O Streams
    3. 3.3. Framing and Parsing
    4. 3.4. Java-Specific Encodings
    5. 3.5. Constructing and Parsing Protocol Messages
      1. 3.5.1. Text-Based Representation
      2. 3.5.2. Binary Representation
      3. 3.5.3. Sending and Receiving
    6. 3.6. Wrapping Up
    7. 3.7. Exercises
  7. 4. Beyond the Basics
    1. 4.1. Multitasking
      1. 4.1.1. Java Threads
      2. 4.1.2. Server Protocol
      3. 4.1.3. Thread-per-Client
      4. 4.1.4. Thread Pool
      5. 4.1.5. System-Managed Dispatching: The Executor Interface
    2. 4.2. Blocking and Timeouts
      1. 4.2.1. accept(), read(), and receive()
      2. 4.2.2. Connecting and Writing
      3. 4.2.3. Limiting Per-Client Time
    3. 4.3. Multiple Recipients
      1. 4.3.1. Broadcast
      2. 4.3.2. Multicast
    4. 4.4. Controlling Default Behaviors
      1. 4.4.1. Keep-Alive
      2. 4.4.2. Send and Receive Buffer Size
      3. 4.4.3. Timeout
      4. 4.4.4. Address Reuse
      5. 4.4.5. Eliminating Buffering Delay
      6. 4.4.6. Urgent Data
      7. 4.4.7. Lingering after close
      8. 4.4.8. Broadcast Permission
      9. 4.4.9. Traffic Class
      10. 4.4.10. Performance-Based Protocol Selection
    5. 4.5. Closing Connections
    6. 4.6. Applets
    7. 4.7. Wrapping Up
    8. 4.8. Exercises
  8. 5. NIO
    1. 5.1. Why Do We Need This?
    2. 5.2. Using Channels with Buffers
    3. 5.3. Selectors
    4. 5.4. Buffers in Detail
      1. 5.4.1. Buffer Indices
      2. 5.4.2. Buffer Creation
      3. 5.4.3. Storing and Retrieving Data
      4. 5.4.4. Preparing Buffers: clear(), flip(), and rewind()
      5. 5.4.5. Compacting Data in a Buffer
      6. 5.4.6. Buffer Perspectives: duplicate(), slice(), etc.
      7. 5.4.7. Character Coding
    5. 5.5. Stream (TCP) Channels in Detail
    6. 5.6. Selectors in Detail
      1. 5.6.1. Registering Interest in Channels
      2. 5.6.2. Selecting and Identifying Ready Channels
      3. 5.6.3. Channel Attachments
      4. 5.6.4. Selectors in a Nutshell
    7. 5.7. Datagram (UDP) Channels
    8. 5.8. Exercises
  9. 6. Under the Hood
    1. 6.1. Buffering and TCP
    2. 6.2. Deadlock Danger
    3. 6.3. Performance Implications
    4. 6.4. TCP Socket Life Cycle
      1. 6.4.1. Connecting
      2. 6.4.2. Closing a TCP Connection
    5. 6.5. Demultiplexing Demystified
    6. 6.6. Exercises
  10. Bibliography