Java NIO ServerSocketChannel
来源:互联网 发布:笑傲江湖 许晴 知乎 编辑:程序博客网 时间:2024/05/16 14:44
A Java NIO ServerSocketChannel is a channel that can listen for incoming TCP connections, just like aServerSocket
in standard Java Networking. The ServerSocketChannel
class is located in the java.nio.channels
package.
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();
Closing a ServerSocketChannel
Closing a ServerSocketChannel
is 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 theaccept()
method returns, it returns a SocketChannel
with 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 true
inside the while-loop.
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. 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(10-ServerSocketChannel)
- Java NIO ServerSocketChannel
- Java NIO ServerSocketChannel
- Java NIO (九) ServerSocketChannel
- Java NIO ServerSocketChannel
- Java NIO ServerSocketChannel
- Java NIO 之 ServerSocketChannel
- Java NIO ServerSocketChannel
- Java Socket:Java-NIO-ServerSocketChannel
- java NIO教程(九)--ServerSocketChannel
- Java NIO 之 ServerSocketChannel SocketChannel
- Java NIO之ServerSocketChannel类
- java NIO selector ServerSocketChannel 例子
- 【JAVA】【NIO】10、Java NIO ServerSocketChannel
- Java Nio 十、Java NIO ServerSocketChannel
- NIO ServerSocketChannel
- 《Java 源码分析》:Java NIO 之 ServerSocketChannel
- linux etc下的profile和/etc/bashrc
- Android使用Pull生成/解析XML文件
- 学习java编程需要掌握哪些核心点
- 怎么删除文件名称过长的文件(我的随记)
- leetcode6. ZigZag Conversion
- Java NIO ServerSocketChannel
- Java 类中各成分加载顺序 和 内存中的存放位置
- 数据结构与算法(一)——红黑树
- 字符串与整数的相互转换atoi 和 atio
- poj 1144 Network 无向连通图求割点
- ROS无法定位软件包解决方法
- 一张图理解网页布局
- getMethods和getDeclaredMethods的区别
- 解决eclipse在创建maven项目时,不断加载问题