Java NIO and TCP Connections: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
==Server==
==Server==


The server code uses a [[Java_Non-Blocking_I/O_Concepts#Selector|Selector]] to multiplex over two [[Java_Non-Blocking_I/O_Concepts#Selectable_Channel|selectable channels]]: a [[Java_Non-Blocking_I/O_Concepts#ServerSocketChannel|ServerSocketChannel]] that listens for incoming network connections and creates new [[Java_Non-Blocking_I/O_Concepts#SocketChannel|SocketChannels]] for each new TCP connection.
The server code uses a [[Java_Non-Blocking_I/O_Concepts#Selector|Selector]] to multiplex over two [[Java_Non-Blocking_I/O_Concepts#Selectable_Channel|selectable channels]]: a [[Java_Non-Blocking_I/O_Concepts#ServerSocketChannel|ServerSocketChannel]] that listens for incoming network connections and creates new [[Java_Non-Blocking_I/O_Concepts#SocketChannel|SocketChannels]] for each new TCP connection. Once a new connection is detected, the selector thread gets the corresponding SocketChannel and register it with the same selector.


==Client==
==Client==

Revision as of 18:31, 25 July 2018

Internal

Overview

This article describes the programming model involved in establishing a simple TCP connection and interacting with it with non-blocking I/O, from Java. We use Java NIO APIs primitives introduced in Java 4.

Programming Model

Server

The server code uses a Selector to multiplex over two selectable channels: a ServerSocketChannel that listens for incoming network connections and creates new SocketChannels for each new TCP connection. Once a new connection is detected, the selector thread gets the corresponding SocketChannel and register it with the same selector.

Client

JavaNIOAndTCPConnections.png

Example

Playground Java NIO and TCP Connections