Java NIO ServerSocketChannel
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.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 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 the accept() 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...
}
}
Ref:
http://tutorials.jenkov.com/java-nio/server-socket-channel.html
Java NIO ServerSocketChannel的更多相关文章
- 【JAVA】【NIO】10、Java NIO ServerSocketChannel
Java NIO的ServerSocketChannel是用来监听外来TCP连接的channel,就想标准Java网络中的ServerSocket.实比例如以下: ServerSocketChanne ...
- Java NIO学习笔记六 SocketChannel 和 ServerSocketChannel
Java NIO SocketChannel Java NIO SocketChannel是连接到TCP网络socket(套接字)的通道.Java NIO相当于Java Networking的sock ...
- Java NIO 完全学习笔记(转)
本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java N ...
- Java NIO 学习总结 学习手册
原文 并发编程网(翻译):http://ifeve.com/java-nio-all/ 源自 http://tutorials.jenkov.com/java-nio/index.html Java ...
- Java NIO学习系列二:Channel
上文总结了Java NIO中的Buffer相关知识点,本文中我们来总结一下它的好兄弟:Channel.上文有说到,Java NIO中的Buffer一般和Channel配对使用,NIO中的所有IO都起始 ...
- Java NIO系列教程(九) ServerSocketChannel
Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.ServerSocketChannel类在 jav ...
- 【Java NIO的深入研究】 ServerSocketChannel
Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.ServerSocketChannel类在 jav ...
- 源码分析netty服务器创建过程vs java nio服务器创建
1.Java NIO服务端创建 首先,我们通过一个时序图来看下如何创建一个NIO服务端并启动监听,接收多个客户端的连接,进行消息的异步读写. 示例代码(参考文献[2]): import java.io ...
- JAVA NIO学习笔记1 - 架构简介
最近项目中遇到不少NIO相关知识,之前对这块接触得较少,算是我的一个盲区,打算花点时间学习,简单做一点个人学习总结. 简介 NIO(New IO)是JDK1.4以后推出的全新IO API,相比传统IO ...
随机推荐
- 安装redis时候的坑
安装redis时候的坑 随手记录一下,以此为借鉴. 1.按照需求下载redis服务 redis可视化文件 2.安装及环境搭建 修改服务里redis.windows-service.conf文件,或者r ...
- codevs 2804 最大最小数质因数
题目描述 Description 先输入n,n<=20;再依次输入n个不同的数,每个数<=1000000;找出最大数和最小数,并输出最大最小数的质因数,没有质因数则输出0. 输入描述 In ...
- BZOJ.2462.[BeiJing2011]矩阵模板(二维Hash)
题目链接 序列上的Hash和前缀和差不多,二维Hash也和二维前缀和差不多了. 预处理大矩阵所有r*c的小矩阵hash值,再对询问的矩阵Hash. 类比于序列上\(s[r]-s[l-1]*pow[r- ...
- 菜鸟nginx源码剖析
菜鸟nginx源码剖析 配置与部署篇(一) 手把手配置nginx "I love you" TCMalloc 对MYSQL 性能 优化的分析 菜鸟nginx源码剖析系列文章解读 A ...
- poj 3660 传递闭包 **
题意:题目给出了m对的相对关系,求有多少个排名是确定的. 链接:点我 如果这个点到其他点的关系是确定的,那么这个点就是确定的,注意如果这个点到不了其他点,但其他点能到这个点,那么这个点和其他点的关系是 ...
- hdu 4442 37届金华赛区 A题
题意:给出一些队伍,每个队伍有初始等待时间和每秒增加的时间,求最短时间 假设有两个队初始时间和每秒增加时间为a1,b1和a2,b2 若第选择第一个的时间小于第二个,则 a1+a2+a1*b2<a ...
- ReentrantLock源码了解
1).ReentrantLock.tryLock //获取没有被其他线程持有的锁 //1).当没有被任何线程持有时,首先将计数器设置为1,并设置当前持有锁的线程为当前线程,最后返回true //2). ...
- LPC43xx MCU PIN Name and GPIO PIN Name Table
//--------------------------------------------------------------------------------+ // LPC43xx Pin N ...
- JTAG Communications model
https://en.wikipedia.org/wiki/Joint_Test_Action_Group In JTAG, devices expose one or more test acces ...
- LINUX 内核学习博客
http://www.cnblogs.com/yjf512/category/385367.html