Java NIO series Tutorials - best practices of SocketChannel

SocketChannel in Java NIO is a channel connected to TCP network socket.

SocketChannel can be created as follows:

  • Open a SocketChannel and connect to a server on the network
  • When a new connection reaches the ServerSocketChannel, a SocketChannel will be created

Open SocketChannel

The following is a simple usage of the opening method of SocketChannel:

SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));

See how Tomcat#NioEndpoint is used

ServerSocketChannel serverSock = ServerSocketChannel.open();
socketProperties.setProperties(serverSock.socket());
InetSocketAddress addr = new InetSocketAddress(getAddress(), getPortWithOffset());
serverSock.socket().bind(addr,getAcceptCount());

Close SocketChannel

When SocketChannel is used, SocketChannel. is called. Close() close socketchannel:

socketChannel.close();

Reading data from SocketChannel

To read data from SocketChannel, call one of the read() methods. As follows:

ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = socketChannel.read(buf);

First, allocate a Buffer. The data read from SocketChannel will be put into this Buffer.

Then, call SocketChannel.. read(). This method reads data from socketchannel to Buffer. The int value returned by the read () method indicates how many bytes have been read into the Buffer. If - 1 is returned, it indicates that the end of the stream has been read (the connection is closed).

Write to SocketChannel

The SocketChannel is used to write data to the SocketChannel Write(), which takes a Buffer as a parameter.

As follows:

String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()) {
    channel.write(buf);
}

Pay attention to SocketChannel The write () method is called in a while loop. The write () method cannot guarantee how many bytes can be written to SocketChannel. Therefore, we call write () repeatedly until the Buffer has no bytes to write.

Non blocking mode

You can set SocketChannel to non blocking mode. Once set, you can call connect(), read() and write() in asynchronous mode.

connect()

If SocketChannel is in non blocking mode, call connect() at this time. This method may return before the connection is established. To determine whether a connection is established, you can call the method of finishConnect(). like this:

socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));
while(! socketChannel.finishConnect() ){
    //wait, or do something else...
}

write()

In non blocking mode, the write() method may return when nothing has been written out. So you need to call write() in the loop. There are already examples, so I won't repeat them here.

read()

In non blocking mode, the read() method may return without reading any data. So you need to pay attention to its int return value, which will tell you how many bytes have been read.

Non blocking mode and Selector

Non blocking mode works better with Selectors. By registering one or more socketchannels with the Selector, you can ask the Selector which channel is ready for reading, writing, etc.

reference resources

  • http://tutorials.jenkov.com/java-nio/socketchannel.html

Keywords: Tomcat

Added by chooseodie on Tue, 18 Jan 2022 22:54:39 +0200