nio

Java Nio Socket Example

Photo of Nassos HasiotisNassos HasiotisNovember 27th, 2015Last Updated: March 19th, 2019
2 1,877 6 minutes read

This article introduces theSocketChannel class and its basic usage. This class is defined in the java.nio package.
 
 
 
 
 
 
 
 
 
 
 

1. Standard Java sockets

Socket programming involves two systems communicating with one another. In implementations prior to NIO, Java TCP client socket code is handled by the java.net.Socket class. A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes,Socket andServerSocket, that implement the client side of the connection and the server side of the connection, respectively. The below image illustrates the nature of this communication:

socketExample

A socket is basically a blocking input/output device. It makes the thread that is using it to block on reads and potentially also block on writes if the underlying buffer is full. Therefore, different threads are required if the server has many open sockets. From a simplistic perspective, the process of a blocking socket communication is as follows:

  • Create aServerSocket, specifying a port to listen on.
  • Invoke the ServerSocket’saccept() method to listen on the configured port for a client connection.
  • When a client connects to the server, theaccept() method returns aSocket through which the server can communicate with the client: anInputStream is obtained to read from the client and anOutputStream to write to the client.

2. Nonblocking SocketChannel with java.nio

With the standard java sockets, if the server needed to be scalable, the socket had to be passed to another thread for processing so that the server could continue listening for additional connections, meaning call the ServerSocket’saccept() method again to listen for another connection.

ASocketChannel on the other hand is a non-blocking way to read from sockets, so that you can have one thread communicate with multiple open connections at once. With socket channel we describe the communication channel between client and server. It is identified by the server IP address and the port number. Data passes through the socket channel by buffer items. A selector monitors the recorded socket channels and serializes the requests, which the server has to satisfy. The Keys describe the objects used by the selector to sort the requests. Each key represents a single client sub-request and contains information to identify the client and the type of the request. With non-blocking I/O, someone can program networked applications to handle multiple simultaneous connections without having to manage multiple thread collection, while also taking advantage of the new server scalability that is built in to java.nio. The below image illustrates this procedure:
socketchannel

3. Example

The following example shows the use ofSocketChannel for creating a simple echo server, meaning it echoes back any message it receives.

3.1. The Server code

import java.io.IOException;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;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.*;public class SocketServerExample {private Selector selector;    private Map<SocketChannel,List> dataMapper;    private InetSocketAddress listenAddress;        public static void main(String[] args) throws Exception {    Runnable server = new Runnable() {@Overridepublic void run() { try {new SocketServerExample("localhost", 8090).startServer();} catch (IOException e) {e.printStackTrace();}}};Runnable client = new Runnable() {@Overridepublic void run() { try { new SocketClientExample().startClient();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}};       new Thread(server).start();       new Thread(client, "client-A").start();       new Thread(client, "client-B").start();    }    public SocketServerExample(String address, int port) throws IOException {    listenAddress = new InetSocketAddress(address, port);        dataMapper = new HashMap<SocketChannel,List>();    }    // create server channel    private void startServer() throws IOException {        this.selector = Selector.open();        ServerSocketChannel serverChannel = ServerSocketChannel.open();        serverChannel.configureBlocking(false);        // retrieve server socket and bind to port        serverChannel.socket().bind(listenAddress);        serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);        System.out.println("Server started...");        while (true) {            // wait for events            this.selector.select();            //work on selected keys            Iterator keys = this.selector.selectedKeys().iterator();            while (keys.hasNext()) {                SelectionKey key = (SelectionKey) keys.next();                // this is necessary to prevent the same key from coming up                 // again the next time around.                keys.remove();                if (!key.isValid()) {                    continue;                }                if (key.isAcceptable()) {                    this.accept(key);                }                else if (key.isReadable()) {                    this.read(key);                }            }        }    }    //accept a connection made to this channel's socket    private void accept(SelectionKey key) throws IOException {        ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();        SocketChannel channel = serverChannel.accept();        channel.configureBlocking(false);        Socket socket = channel.socket();        SocketAddress remoteAddr = socket.getRemoteSocketAddress();        System.out.println("Connected to: " + remoteAddr);        // register channel with selector for further IO        dataMapper.put(channel, new ArrayList());        channel.register(this.selector, SelectionKey.OP_READ);    }        //read from the socket channel    private void read(SelectionKey key) throws IOException {        SocketChannel channel = (SocketChannel) key.channel();        ByteBuffer buffer = ByteBuffer.allocate(1024);        int numRead = -1;        numRead = channel.read(buffer);        if (numRead == -1) {            this.dataMapper.remove(channel);            Socket socket = channel.socket();            SocketAddress remoteAddr = socket.getRemoteSocketAddress();            System.out.println("Connection closed by client: " + remoteAddr);            channel.close();            key.cancel();            return;        }        byte[] data = new byte[numRead];        System.arraycopy(buffer.array(), 0, data, 0, numRead);        System.out.println("Got: " + new String(data));    }}

From the above code:

  • In themain() method on lines 43-45, one thread for creating theServerSocketChannel is started and two client threads responsible for starting the clients which will create aSocketChannel for sending messsages to the server.
    new Thread(server).start();new Thread(client, "client-A").start();new Thread(client, "client-B").start();
  • In thestartServer() method on line 54,  the serverSocketChannel is created as nonBlocking, the server socket is retrieved and bound to the specified port:
     ServerSocketChannel serverChannel = ServerSocketChannel.open();serverChannel.configureBlocking(false);// retrieve server socket and bind to portserverChannel.socket().bind(listenAddress);

    Finally, theregister method associates the selector to the socket channel.

    Want to be a Java NIO Master ?
    Subscribe to our newsletter and download the JDBCUltimateGuideright now!
    In order to help you master Java NIO Library, we have compiled a kick-ass guide with all the major Java NIO features and use cases! Besides studying them online you may download the eBook in PDF format!

    Thank you!

    We will contact you soon.

    serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);

    The second parameter represents the type of the registration. In this case, we useOP_ACCEPT, which means the selector merely reports that a client attempts a connection to the server. Other possible options are:OP_CONNECT, which will be used by the client;OP_READ; andOP_WRITE.
    After that, theselect method is used on line 67, which blocks the execution and waits for events recorded on the selector in an infinite loop.

    this.selector.select();
  • The selector waits for events and creates the keys. According to the key-types, an opportune operation is performed. There are four possible types for a key:
     

    • Acceptable: the associated client requests a connection.
    • Connectable: the server accepted the connection.
    • Readable: the server can read.
    • Writeable: the server can write.
  • If an acceptable key is found, theaccept(SelectionKey key) on line 93 is invoked in order to create a channel which accepts this connection, creates a standard java socket on line 97 and register the channel with the selector:
    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();SocketChannel channel = serverChannel.accept();channel.configureBlocking(false);Socket socket = channel.socket();SocketAddress remoteAddr = socket.getRemoteSocketAddress();
  • After receiving a readable key from the client, the read(SelectionKey key) is called on line 107 which reads from the socket channel. A byte buffer is allocated for reading from the channel
    numRead = channel.read(buffer);

    and the client’s transmitted data are echoed on System.out:

    System.out.println("Got: " + new String(data));

3.2. The Client code

import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;public class SocketClientExample {     public void startClient()            throws IOException, InterruptedException {         InetSocketAddress hostAddress = new InetSocketAddress("localhost", 8090);        SocketChannel client = SocketChannel.open(hostAddress);         System.out.println("Client... started");                String threadName = Thread.currentThread().getName();         // Send messages to server        String [] messages = new String []         {threadName + ": test1",threadName + ": test2",threadName + ": test3"};         for (int i = 0; i < messages.length; i++) {            byte [] message = new String(messages [i]).getBytes();            ByteBuffer buffer = ByteBuffer.wrap(message);            client.write(buffer);            System.out.println(messages [i]);            buffer.clear();            Thread.sleep(5000);        }        client.close();                }}
  • In the above client code, each client thread creates a socket channel on the server’s host address on line 12:
    SocketChannel client = SocketChannel.open(hostAddress);
  • On line 19, a String array is created to be transmitted to the server using the previously created socket. The data contain also each thread’s name for distinguishing the sender:
    String threadName = Thread.currentThread().getName();// Send messages to serverString [] messages = new String [] {threadName + ": test1",threadName + ": test2",threadName + ": test3"};
  • For each string message a buffer is created on line 24:
    ByteBuffer buffer = ByteBuffer.wrap(message);

    and each message is written to the channel from the given buffer on line 25:

    ByteBuffer buffer = ByteBuffer.wrap(message);

3.3. The output

Server started...Client... startedClient... startedclient-A: test1client-B: test1Connected to: /127.0.0.1:51468Got: client-B: test1Connected to: /127.0.0.1:51467Got: client-A: test1client-A: test2client-B: test2Got: client-B: test2Got: client-A: test2client-A: test3client-B: test3Got: client-B: test3Got: client-A: test3Connection closed by client: /127.0.0.1:51468Connection closed by client: /127.0.0.1:51467

4. Download Java Source Code

This was an example ofjava.nio.SocketChannel

Download
You can download the full source code of this example here:SocketExampleNio.zip
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Nassos HasiotisNassos HasiotisNovember 27th, 2015Last Updated: March 19th, 2019
2 1,877 6 minutes read
Photo of Nassos Hasiotis

Nassos Hasiotis

Nassos has graduated from Computer Engineering and Informatics Department in the University of Patras. He also holds a Master degree in Communication and Network Systems from University of Athens. He has a 10-year work experience as a Java and C++ developer in the Telecommunication and IT industry participating in various projects. He currently works as a senior software developer in the IT sector where he is mainly involved with projects for the EU requiring extensive java skills.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.