Java NiO Series Tutorial (ix) Serversocketchannel

Source: Internet
Author: User

Original link Jakob Jenkov Translator: Zheng Yuting proofreading: Ding

The Serversocketchannel in Java NiO is a channel that can listen for incoming TCP connections, just like serversocket in standard IO. The Serversocketchannel class is in the Java.nio.channels package.


Here's an example:

01 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
02
03 serverSocketChannel.socket().bind(newInetSocketAddress(9999));
04
05 while(true){
06     SocketChannel socketChannel =
07             serverSocketChannel.accept();
08
09     //do something with socketChannel...
10 }
Open Serversocketchannel

Open the Serversocketchannel by calling the Serversocketchannel.open () method. For example:

1 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
Close Serversocketchannel

Close Serversocketchannel by calling the Serversocketchannel.close () method. Such as:

1 serverSocketChannel.close();
Listening for new Incoming connections

The new incoming connection is monitored by the serversocketchannel.accept () method. When the Accept () method returns, it returns a Socketchannel that contains the new incoming connection. Therefore, the accept () method is blocked until a new connection arrives.

Typically, you will not just listen to one connection, but call the Accept () method in the while loop. As in the following example:

1 while(true){
2     SocketChannel socketChannel =
3             serverSocketChannel.accept();
4
5     //do something with socketChannel...
6 }

Of course, you can also use exit criteria other than true in the while loop.

Non-blocking mode

Serversocketchannel can be set to non-blocking mode. In nonblocking mode, the Accept () method returns immediately, or null if no new incoming connection has been entered. Therefore, you need to check if the returned Socketchannel is null.

01 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
02
03 serverSocketChannel.socket().bind(newInetSocketAddress(9999));
04 serverSocketChannel.configureBlocking(false);
05
06 while(true){
07     SocketChannel socketChannel =
08             serverSocketChannel.accept();
09
10     if(socketChannel != null){
11         //do something with socketChannel...
12     }
13 }

original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java NiO Series Tutorial (ix) Serversocketchannel

Java NiO Series Tutorial (ix) Serversocketchannel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.