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 作為服務(wù)端,用于監(jiān)聽(tīng)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();
創(chuàng)建一個(gè)ServerSocketChannel實(shí)例椿肩,使用open方法
Closing a ServerSocketChannel
Closing a ServerSocketChannel
is done by calling the ServerSocketChannel.close()
method. Here is how that looks:
serverSocketChannel.close();
關(guān)閉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.
當(dāng) accept()方法返回的時(shí)候,它返回一個(gè)包含新進(jìn)來(lái)的連接的 SocketChannel剔宪。因此, accept()方法會(huì)一直阻塞到有新連接到達(dá)中跌。
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.
當(dāng)然,也可以在while循環(huán)中使用除了true以外的其它退出準(zhǔn)則慢逾。
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可以設(shè)置成非阻塞模式晋渺。在非阻塞模式下窄驹,accept() 方法會(huì)立刻返回违柏,如果還沒(méi)有新進(jìn)來(lái)的連接,返回的將是null博烂。 因此,需要檢查返回的SocketChannel是否是null
Here is an example:
S public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); // 初始化一個(gè)
serverSocketChannel.socket().bind(new InetSocketAddress(8989)); // 綁定端口
// 在Blocking模式下accept會(huì)一直阻塞漱竖,直到有connection進(jìn)來(lái)禽篱。但是在Non-Blocking模型下,accept會(huì)馬上返回馍惹,但是可能返回的是null
serverSocketChannel.configureBlocking(false);
while (true) { // 做個(gè)循環(huán)
SocketChannel socketChannel = serverSocketChannel.accept(); // 獲取到client channel
if (null != socketChannel) {
System.out.println("connected channel");
// 讀取數(shù)據(jù)
} else {
// System.out.println("there is no connection now!");
}
}
}