您的位置:首页 > 编程语言 > Java开发

Java NIO系列教程(九) ServerSocketChannel

2017-04-12 20:50 197 查看
本文转载至:http://ifeve.com/server-socket-channel/

Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样。ServerSocketChannel类在 java.nio.channels包中。

这里有个例子:

01
ServerSocketChannel
serverSocketChannel = ServerSocketChannel.open();
02
 
03
serverSocketChannel.socket().bind(
new
 
InetSocketAddress(
9999
));
04
 
05
while
(
true
){
06
    
SocketChannel
socketChannel =
07
            
serverSocketChannel.accept();
08
 
09
    
//do
something with socketChannel...
10
}


打开 ServerSocketChannel

通过调用 ServerSocketChannel.open() 方法来打开ServerSocketChannel.如:

1
ServerSocketChannel
serverSocketChannel = ServerSocketChannel.open();


关闭 ServerSocketChannel

通过调用ServerSocketChannel.close() 方法来关闭ServerSocketChannel. 如:

1
serverSocketChannel.close();


监听新进来的连接

通过 ServerSocketChannel.accept() 方法监听新进来的连接。当 accept()方法返回的时候,它返回一个包含新进来的连接的 SocketChannel。因此, accept()方法会一直阻塞到有新连接到达。

通常不会仅仅只监听一个连接,在while循环中调用 accept()方法. 如下面的例子:

1
while
(
true
){
2
    
SocketChannel
socketChannel =
3
            
serverSocketChannel.accept();
4
 
5
    
//do
something with socketChannel...
6
}
当然,也可以在while循环中使用除了true以外的其它退出准则。


非阻塞模式

ServerSocketChannel可以设置成非阻塞模式。在非阻塞模式下,accept() 方法会立刻返回,如果还没有新进来的连接,返回的将是null。 因此,需要检查返回的SocketChannel是否是null.如:

查看源代码

打印帮助

01
ServerSocketChannel
serverSocketChannel = ServerSocketChannel.open();
02
 
03
serverSocketChannel.socket().bind(
new
 
InetSocketAddress(
9999
));
04
serverSocketChannel.configureBlocking(
false
);
05
 
06
while
(
true
){
07
    
SocketChannel
socketChannel =
08
            
serverSocketChannel.accept();
09
 
10
    
if
(socketChannel
!= 
null
){
11
        
//do
something with socketChannel...
12
    
}
13
}
java 示例代码:

package com.zzg.nio.server;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class NioServer {
// 通道管理器
private Selector selector;

public void initServer(int port) throws Exception {
// 获得一个ServerSocket通道
ServerSocketChannel serverChannel = ServerSocketChannel.open();
// 设置通道为 非阻塞
serverChannel.configureBlocking(false);
// 将该通道对于的serverSocket绑定到port端口
serverChannel.socket().bind(new InetSocketAddress(port));
// 获得一耳光通道管理器
this.selector = Selector.open();

// 将通道管理器和该通道绑定,并为该通道注册selectionKey.OP_ACCEPT事件
// 注册该事件后,当事件到达的时候,selector.select()会返回,
// 如果事件没有到达selector.select()会一直阻塞

serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}

// 采用轮训的方式监听selector上是否有需要处理的事件,如果有,进行处理
public void listen() throws Exception {
System.out.println("start server");
// 轮询访问selector
while (true) {
// 当注册事件到达时,方法返回,否则该方法会一直阻塞
selector.select();
// 获得selector中选中的相的迭代器,选中的相为注册的事件
Iterator ite = this.selector.selectedKeys().iterator();
while (ite.hasNext()) {
SelectionKey key = (SelectionKey) ite.next();
// 删除已选的key 以防重负处理
ite.remove();
// 客户端请求连接事件
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
// 获得和客户端连接的通道
SocketChannel channel = server.accept();
// 设置成非阻塞
channel.configureBlocking(false);
// 在这里可以发送消息给客户端
channel.write(ByteBuffer.wrap(new String("hello client").getBytes()));
// 在客户端 连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限
channel.register(this.selector, SelectionKey.OP_READ);
// 获得了可读的事件

} else if (key.isReadable()) {
read(key);
}

}
}
}

// 处理 读取客户端发来的信息事件
private void read(SelectionKey key) throws Exception {
// 服务器可读消息,得到事件发生的socket通道
SocketChannel channel = (SocketChannel) key.channel();
// 穿件读取的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(18);
channel.read(buffer);
byte[] data = buffer.array();
String msg = new String(data).trim();
System.out.println("server receive from client: " + msg);
ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes());
channel.write(outBuffer);
}

public static void main(String[] args) throws Throwable {
NioServer server = new NioServer();
server.initServer(8989);
server.listen();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: