Java Network Tutorial - Java Non-Blocking Socket








With non-blocking socket channels, we have to change our way of thinking about execution sequence.

Server Socket Channel

To create a selector object, call its open() static method.

Selector selector = Selector.open();

A ServerSocketChannel is used to listen for a new connection request from clients.

Call its open() static method to create a ServerSocketChannel.

ServerSocketChannel ssChannel = ServerSocketChannel.open();

By default, a server socket channel or a socket channel is a blocking channel. To make it a non-blocking channel, call the following method.

ssChannel.configureBlocking(false);




Selector

The server socket has to register with the selector to do certain operations.

There are four kinds of operations we can register a channel with the selector.

  • Connect operation with SelectionKey.OP_CONNECT, which can be registered for SocketChannel at client. Selector will notify about the connect operation progress.
  • Accept operation with SelectionKey.OP_ACCEPT, which can be registered for ServerSocketChannel at server. Selector will notify when a client request for a new connection arrives.
  • Read operation with SelectionKey.OP_READ, which can be registered for SocketChannel at client and server. Selector will notify when the channel is ready to read some data.
  • Write operation with SelectionKey.OP_WRITE, which can be registered for SocketChannel at client and server. Selector will notify when channel is ready to write some data.




Example

The following code shows how to create a Non-Blocking Socket Channel Echo Server Program.

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
/*www .jav a 2  s.  c o  m*/
public class Main {
  public static void main(String[] args) throws Exception {
    InetAddress hostIPAddress = InetAddress.getByName("localhost");
    int port = 19000;
    Selector selector = Selector.open();
    ServerSocketChannel ssChannel = ServerSocketChannel.open();
    ssChannel.configureBlocking(false);
    ssChannel.socket().bind(new InetSocketAddress(hostIPAddress, port));
    ssChannel.register(selector, SelectionKey.OP_ACCEPT);
    while (true) {
      if (selector.select() <= 0) {
        continue;
      }
      processReadySet(selector.selectedKeys());
    }
  }
  public static void processReadySet(Set readySet) throws Exception {
    Iterator iterator = readySet.iterator();
    while (iterator.hasNext()) {
      SelectionKey key = (SelectionKey) iterator.next();
      iterator.remove();
      if (key.isAcceptable()) {
        ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();
        SocketChannel sChannel = (SocketChannel) ssChannel.accept();
        sChannel.configureBlocking(false);
        sChannel.register(key.selector(), SelectionKey.OP_READ);
      }
      if (key.isReadable()) {
        String msg = processRead(key);
        if (msg.length() > 0) {
          SocketChannel sChannel = (SocketChannel) key.channel();
          ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
          sChannel.write(buffer);
        }
      }
    }
  }
  public static String processRead(SelectionKey key) throws Exception {
    SocketChannel sChannel = (SocketChannel) key.channel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    int bytesCount = sChannel.read(buffer);
    if (bytesCount > 0) {
      buffer.flip();
      return new String(buffer.array());
    }
    return "NoMessage";
  }
}

Example 2

A Non-Blocking Socket Channel Echo Client Program

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Iterator;
import java.util.Set;
/*from ww w .j a  va  2  s . co m*/
public class Main {
  static BufferedReader userInputReader = null;

  public static boolean processReadySet(Set readySet) throws Exception {
    Iterator iterator = readySet.iterator();
    while (iterator.hasNext()) {
      SelectionKey key = (SelectionKey)
       iterator.next();
      iterator.remove();
      if (key.isConnectable()) {
        boolean connected = processConnect(key);
        if (!connected) {
          return true; // Exit
        }
      }
      if (key.isReadable()) {
        String msg = processRead(key);
        System.out.println("[Server]: " + msg);
      }
      if (key.isWritable()) {
        System.out.print("Please enter a message(Bye to quit):");
        String msg = userInputReader.readLine();
        
        if (msg.equalsIgnoreCase("bye")) {
          return true; // Exit
        }
        SocketChannel sChannel = (SocketChannel) key.channel();
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
        sChannel.write(buffer);
      }
    }
    return false; // Not done yet
  }
  public static boolean processConnect(SelectionKey key) throws Exception{
    SocketChannel channel = (SocketChannel) key.channel();
    while (channel.isConnectionPending()) {
      channel.finishConnect();
    }
    return true;
  }
  public static String processRead(SelectionKey key) throws Exception {
    SocketChannel sChannel = (SocketChannel) key.channel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    sChannel.read(buffer);
    buffer.flip();
    Charset charset = Charset.forName("UTF-8");
    CharsetDecoder decoder = charset.newDecoder();
    CharBuffer charBuffer = decoder.decode(buffer);
    String msg = charBuffer.toString();
    return msg;
  }
  public static void main(String[] args) throws Exception {
    InetAddress serverIPAddress = InetAddress.getByName("localhost");
    int port = 19000;
    InetSocketAddress serverAddress = new InetSocketAddress(
        serverIPAddress, port);
    Selector selector = Selector.open();
    SocketChannel channel = SocketChannel.open();
    channel.configureBlocking(false);
    channel.connect(serverAddress);
    int operations = SelectionKey.OP_CONNECT | SelectionKey.OP_READ
        | SelectionKey.OP_WRITE;
    channel.register(selector, operations);

    userInputReader = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
      if (selector.select() > 0) {
        boolean doneStatus = processReadySet(selector.selectedKeys());
        if (doneStatus) {
          break;
        }
      }
    }
    channel.close();
  }
}