Java NIO and TCP Connections: Difference between revisions
(→Server) |
(→Server) |
||
Line 12: | Line 12: | ||
==Server== | ==Server== | ||
The server code uses a [[Java_Non-Blocking_I/O_Concepts#Selector|Selector]] to multiplex over | The server code uses a [[Java_Non-Blocking_I/O_Concepts#Selector|Selector]] to multiplex over selectable channels: [[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, and subsequently registered SocketChannels. | ||
<syntaxhighlight lang='java'> | |||
// | |||
// Main selector multiplexor. We use the main thread as selector thread. | |||
// | |||
Selector selector = Selector.open(); | |||
// | |||
// The ServerSocketChannel used to accept new TCP connections | |||
// | |||
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); | |||
serverSocketChannel.configureBlocking(false); | |||
InetSocketAddress address = new InetSocketAddress(PORT); | |||
ServerSocket ss = serverSocketChannel.socket(); | |||
ss.bind(address); | |||
// | |||
// Register the ServerSocketChannel with the selector | |||
// | |||
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); | |||
</syntaxhighlight> | |||
The main event loop handles two types of events: new connections and data availability on the existing connections. Once a new connection is detected, the selector thread retrieves the corresponding SocketChannel and registers it with the same selector. If data becomes available on any of the registered SocketChannels, we use a [[Java_Non-Blocking_I/O_Concepts#Buffer|Buffer]] to read it. | The main event loop handles two types of events: new connections and data availability on the existing connections. Once a new connection is detected, the selector thread retrieves the corresponding SocketChannel and registers it with the same selector. If data becomes available on any of the registered SocketChannels, we use a [[Java_Non-Blocking_I/O_Concepts#Buffer|Buffer]] to read it. | ||
<syntaxhighlight lang='java'> | |||
// | |||
// The main event loop | |||
// | |||
</syntaxhighlight> | |||
==Client== | ==Client== |
Revision as of 18:45, 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 selectable channels: selectable channels: a ServerSocketChannel that listens for incoming network connections and creates new SocketChannels for each new TCP connection, and subsequently registered SocketChannels.
//
// Main selector multiplexor. We use the main thread as selector thread.
//
Selector selector = Selector.open();
//
// The ServerSocketChannel used to accept new TCP connections
//
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
InetSocketAddress address = new InetSocketAddress(PORT);
ServerSocket ss = serverSocketChannel.socket();
ss.bind(address);
//
// Register the ServerSocketChannel with the selector
//
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
The main event loop handles two types of events: new connections and data availability on the existing connections. Once a new connection is detected, the selector thread retrieves the corresponding SocketChannel and registers it with the same selector. If data becomes available on any of the registered SocketChannels, we use a Buffer to read it.
//
// The main event loop
//