Example usage for java.nio.channels SocketChannel setOption

List of usage examples for java.nio.channels SocketChannel setOption

Introduction

In this page you can find the example usage for java.nio.channels SocketChannel setOption.

Prototype

@Override
public abstract <T> SocketChannel setOption(SocketOption<T> name, T value) throws IOException;

Source Link

Usage

From source file:ee.ria.xroad.proxy.clientproxy.FastestSocketSelector.java

private SocketInfo initConnections(Selector selector) throws IOException {
    log.trace("initConnections()");

    for (URI target : addresses) {
        SocketChannel channel = SocketChannel.open();
        channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
        channel.configureBlocking(false);
        try {//from  ww  w.j a  v  a 2s  . c  om
            channel.register(selector, SelectionKey.OP_CONNECT, target);

            if (channel.connect(new InetSocketAddress(target.getHost(), target.getPort()))) { // connected immediately
                channel.configureBlocking(true);
                return new SocketInfo(target, channel.socket());
            }
        } catch (Exception e) {
            closeQuietly(channel);
            log.trace("Error connecting to '{}': {}", target, e);
        }
    }

    return null;
}