Search in sources :

Example 91 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project robovm by robovm.

the class InterruptedStreamTest method newSocketChannelPair.

/**
     * Returns a pair of connected sockets backed by NIO socket channels.
     */
private Socket[] newSocketChannelPair() throws IOException {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(0));
    SocketChannel clientSocketChannel = SocketChannel.open();
    clientSocketChannel.connect(serverSocketChannel.socket().getLocalSocketAddress());
    SocketChannel server = serverSocketChannel.accept();
    serverSocketChannel.close();
    return new Socket[] { clientSocketChannel.socket(), server.socket() };
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Socket(java.net.Socket)

Example 92 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project CloudStack-archive by CloudStack-extras.

the class NioServer method init.

@Override
protected void init() throws IOException {
    _selector = SelectorProvider.provider().openSelector();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    _localAddr = new InetSocketAddress(_port);
    ssc.socket().bind(_localAddr);
    ssc.register(_selector, SelectionKey.OP_ACCEPT, null);
    s_logger.info("NioConnection started and listening on " + _localAddr.toString());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 93 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project CshBBrain by CshBBrain.

the class MasterServer method accept.

/**
	 * 
	 * <li>方法名:accept
	 * <li>@param key
	 * <li>@param isClusters
	 * <li>@throws IOException
	 * <li>返回类型:void
	 * <li>说明:根据请求创建连接,可以是来自客户端的请求,也可能是来自集群中其他服务器的请求
	 * <li>创建人:CshBBrain, 技术博客:http://cshbbrain.iteye.com/
	 * <li>创建日期:2012-10-21
	 * <li>修改人: 
	 * <li>修改日期:
	 */
private void accept(SelectionKey key, Boolean isClusters) throws IOException {
    // For an accept to be pending the channel must be a server socket channel.
    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
    // Accept the connection and make it non-blocking
    SocketChannel socketChannel = serverSocketChannel.accept();
    // 设置为非阻塞式
    socketChannel.configureBlocking(false);
    // 设置接收缓存
    socketChannel.socket().setReceiveBufferSize(this.sockectReceiveBufferSize * 1024);
    // 设置发送缓存
    socketChannel.socket().setSendBufferSize(this.sockectSendBufferSize * 1024);
    socketChannel.socket().setSoTimeout(0);
    socketChannel.socket().setTcpNoDelay(true);
    if (keepConnect) {
        //使用长连接,保持连接状态
        socketChannel.socket().setKeepAlive(true);
    }
    // 将客户端sockect通道注册到指定的输入监听线程上
    if (isClusters) {
        // 集群请求通道注册
        this.readWriteMonitors.get(Math.abs(this.connectIndex.getAndIncrement()) % this.readWriteMonitors.size()).registeClusters(socketChannel);
    } else {
        // 业务请求通道注册
        this.readWriteMonitors.get(Math.abs(this.connectIndex.getAndIncrement()) % this.readWriteMonitors.size()).registe(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 94 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project CloudStack-archive by CloudStack-extras.

the class NioConnection method accept.

protected void accept(SelectionKey key) throws IOException {
    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
    SocketChannel socketChannel = serverSocketChannel.accept();
    Socket socket = socketChannel.socket();
    socket.setKeepAlive(true);
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Connection accepted for " + socket);
    }
    // Begin SSL handshake in BLOCKING mode
    socketChannel.configureBlocking(true);
    SSLEngine sslEngine = null;
    try {
        SSLContext sslContext = Link.initSSLContext(false);
        sslEngine = sslContext.createSSLEngine();
        sslEngine.setUseClientMode(false);
        sslEngine.setNeedClientAuth(false);
        Link.doHandshake(socketChannel, sslEngine, false);
    } catch (Exception e) {
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Socket " + socket + " closed on read.  Probably -1 returned: " + e.getMessage());
        }
        try {
            socketChannel.close();
            socket.close();
        } catch (IOException ignore) {
        }
        return;
    }
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("SSL: Handshake done");
    }
    socketChannel.configureBlocking(false);
    InetSocketAddress saddr = (InetSocketAddress) socket.getRemoteSocketAddress();
    Link link = new Link(saddr, this);
    link.setSSLEngine(sslEngine);
    link.setKey(socketChannel.register(key.selector(), SelectionKey.OP_READ, link));
    Task task = _factory.create(Task.Type.CONNECT, link, null);
    registerLink(saddr, link);
    _executor.execute(task);
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SSLEngine(javax.net.ssl.SSLEngine) InetSocketAddress(java.net.InetSocketAddress) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Socket(java.net.Socket) CancelledKeyException(java.nio.channels.CancelledKeyException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Example 95 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project spring-boot by spring-projects.

the class TunnelClient method start.

/**
	 * Start the client and accept incoming connections on the port.
	 * @throws IOException in case of I/O errors
	 */
public void start() throws IOException {
    synchronized (this.monitor) {
        Assert.state(this.serverThread == null, "Server already started");
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort));
        logger.trace("Listening for TCP traffic to tunnel on port " + this.listenPort);
        this.serverThread = new ServerThread(serverSocketChannel);
        this.serverThread.start();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)114 SocketChannel (java.nio.channels.SocketChannel)62 InetSocketAddress (java.net.InetSocketAddress)52 IOException (java.io.IOException)41 ByteBuffer (java.nio.ByteBuffer)26 SelectionKey (java.nio.channels.SelectionKey)19 ServerSocket (java.net.ServerSocket)18 Selector (java.nio.channels.Selector)13 Socket (java.net.Socket)12 Test (org.junit.Test)11 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InetAddress (java.net.InetAddress)9 ClosedSelectorException (java.nio.channels.ClosedSelectorException)8 SocketException (java.net.SocketException)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 CancelledKeyException (java.nio.channels.CancelledKeyException)5 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4