Java NIO ServerSocketChannel
2016-03-12 22:49
501 查看
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
standard Java Networking. The
Here is an example:
You open a
Closing a
serverSocketChannel.close();
Listening for incoming connections is done by calling the
blocks until an incoming connection arrives.
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
null. Here is an example:
Closing a ServerSocketChannel
Listening for Incoming Connections
Non-blocking Mode
Jakob Jenkov Last update: 2014-06-25 |
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...
}
}
相关文章推荐
- Spring IOC容器概念
- eclipse下maven项目保持原有目录结构配置resin运行环境
- 20145127《java程序设计》第二周学习总结
- java线程池ThreadPoolExecutor的使用
- Spring MVC中的视图解析ViewResolver
- java语法_1:面向对象:1
- FileReader文件字符输入流
- Java NIO SocketChannel
- Java NIO FileChannel
- eclipse安装extjs插件
- java内部类的可见性总结和java内部类的创建
- Springmvc框架本地化和国际化
- JAVA设计模式--观察者
- Spring中的SpEL
- Java学习
- Java泛型通配符super使用Demo
- Java虚拟机内存模型
- java反射
- spring+shiro+cas的整合问题之循环重定向
- Java System.getProperty()方法获取系统信息