NIO之九--ServerSocketChannel

Java NIO ServerSocketChannel

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!");
            }

        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 小时候村里没多少户人家用煤气灶,一家人烧水做饭全用大铁锅,于是就有许多以砍柴为生的人。 我就认识这样一位砍柴...
    阿列尼阅读 560评论 1 2
  • 新的学期开始了,孩子的心也收回来了,晚上我下班一进门,艺萱就高兴的跑过来说:妈妈,我的作业有听写的,我说:...
    陈艺萱妈妈阅读 109评论 0 0
  • 前天领导过来跟我一起去看一个项目,这是新调来的上级。 一路上没有什么话说,以为就这样了,在回来的路上,领导有提到其...
    会飞的蓉子阅读 794评论 6 5