NIO之九--ServerSocketChannel

Java NIO ServerSocketChannel

  • Opening a ServerSocketChannel
  • Closing a ServerSocketChannel
  • Listening for Incoming Connections
  • Non-blocking Mode

A Java NIO ServerSocketChannel is a channel that can listen for incoming TCP connections, just like a ServerSocket in standard Java Networking. The ServerSocketChannel class is located in the java.nio.channels package.

ServerSocketChannel 作为服务端,用于监听TCP连接并处理

Here is an example:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.socket().bind(new InetSocketAddress(9999));

while(true){
    SocketChannel socketChannel =
            serverSocketChannel.accept();

    //do something with socketChannel...
}

Opening a ServerSocketChannel

You open a ServerSocketChannel by calling the ServerSocketChannel.open() method. Here is how that looks:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

创建一个ServerSocketChannel实例,使用open方法

Closing a ServerSocketChannel

Closing a ServerSocketChannel is done by calling the ServerSocketChannel.close() method. Here is how that looks:

serverSocketChannel.close();

关闭ServerSocketChannel

Listening for Incoming Connections

Listening for incoming connections is done by calling the ServerSocketChannel.accept() method. When the accept() method returns, it returns a SocketChannel with an incoming connection. Thus, the accept()method blocks until an incoming connection arrives.

当 accept()方法返回的时候,它返回一个包含新进来的连接的 SocketChannel。因此, accept()方法会一直阻塞到有新连接到达。

Since you are typically not interested in listening just for a single connection, you call the accept() inside a while-loop. Here is how that looks:

while(true){
    SocketChannel socketChannel =
            serverSocketChannel.accept();

    //do something with socketChannel...
}

Of course you would use some other stop-criteria than true inside the while-loop.

当然,也可以在while循环中使用除了true以外的其它退出准则。

Non-blocking Mode

A ServerSocketChannel can be set into non-blocking mode. In non-blocking mode the accept() method returns immediately, and may thus return null, if no incoming connection had arrived. Therefore you will have to check if the returned SocketChannel is null.

ServerSocketChannel可以设置成非阻塞模式。在非阻塞模式下,accept() 方法会立刻返回,如果还没有新进来的连接,返回的将是null。 因此,需要检查返回的SocketChannel是否是null

Here is an example:

S public static void main(String[] args) throws IOException {

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); // 初始化一个

        serverSocketChannel.socket().bind(new InetSocketAddress(8989)); // 绑定端口

        // 在Blocking模式下accept会一直阻塞,直到有connection进来。但是在Non-Blocking模型下,accept会马上返回,但是可能返回的是null
        serverSocketChannel.configureBlocking(false);

        while (true) { // 做个循环

            SocketChannel socketChannel = serverSocketChannel.accept(); // 获取到client channel

            if (null != socketChannel) {
                System.out.println("connected channel");

                // 读取数据

            } else {
//                System.out.println("there is no connection now!");
            }

        }
    }

你可能感兴趣的:(NIO之九--ServerSocketChannel)