Java NIO ServerSocketChannel
2016-10-27 20:48
507 查看
A Java NIO ServerSocketChannel is a channel that can listen for incoming TCP connections, just like a
standard Java Networking. The
Here is an example:
You open a
Closing a
serverSocketChannel.close();
Listening for incoming connections is done by calling the
an incoming connection. Thus, the
Since you are typically not interested in listening just for a single connection, you call the
Of course you would use some other stop-criteria than
A
you will have to check if the returned
ServerSocketin
standard Java Networking. The
ServerSocketChannelclass is located in the
java.nio.channelspackage.
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 ServerSocketChannelby calling the
ServerSocketChannel.open()method. Here is how that looks:
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
Closing a ServerSocketChannel
Closing a ServerSocketChannelis done by calling the
ServerSocketChannel.close()method. Here is how that looks:
serverSocketChannel.close();
Listening for Incoming Connections
Listening for incoming connections is done by calling the ServerSocketChannel.accept()method. When the
accept()method returns, it returns a
SocketChannelwith
an incoming connection. Thus, the
accept()method blocks until an incoming connection arrives.
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
trueinside the while-loop.
Non-blocking Mode
A ServerSocketChannelcan 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
SocketChannelis null. Here is an example:
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(9999));
serverSocketChannel.configureBlocking(false);
while(true){
SocketChannel socketChannel =
serverSocketChannel.accept();
if(socketChannel != null){
//do something with socketChannel...
}
}
相关文章推荐
- 【Java NIO的深入研究】 ServerSocketChannel
- 【Java NIO的深入研究】 ServerSocketChannel
- Java网络编程之NIO(ServerSocketChannel/SocketChannel)-yellowcong
- java NIO中ServerSocketChannel和SocketChannel连接的过程
- Java基础知识强化之IO流笔记80:NIO之 ServerSocketChannel
- Java NIO —— TCP套接字(ServerSocketChannel & SocketChannel)
- Java Nio 十、Java NIO ServerSocketChannel
- [java]NIO服务器(ServerSocketChannel)开发的两种实现方式
- 《Java 源码分析》:Java NIO 之 ServerSocketChannel
- JAVA-NIO之Socket/ServerSocket Channel(详解)
- Java NIO ServerSocketChannel
- java NIO之ServerSocketChannel的使用
- 【Java NIO的深入研究】 ServerSocketChannel
- Java NIO ServerSocketChannel
- Java netty之NioServerSocketChannel
- Java NIO 学习(四)--ServerSocketChannel与SocketChannel
- 【JAVA】【NIO】10、Java NIO ServerSocketChannel
- 《Java 源码分析》:Java NIO 之 ServerSocketChannel
- Java NIO ServerSocketChannel
- Java NIO ServerSocketChannel