NIO实现非阻塞Socket通信

注:
1.服务端的sk.isAcceptable()只能是注册的惟一一个ServerSocketChannel,所以有sk.interestOps(SelectionKey.OP_ACCEPT);
产生sk.isReadable()只能是ServerSocketChannel产生的并且已经注册过的SocketChannel,所以有sk.interestOps(SelectionKey.OP_READ);  
对于服务端的两行:
sc.register(selector, SelectionKey.OP_READ);  
// 将sk对应的Channel设置成准备接受其他请求(因为它是惟一一个ServerSocketChannel)  
sk.interestOps(SelectionKey.OP_ACCEPT);  
中第1行的SelectionKey不能写成OP_WRITE,否则服务端不能读取数据,程序是我们写的,当然知道这个通道建立起来是要读取数据还是要
写数据,第2行也不能换成其它值,出异常。

2. 客户端的连接方式可以直接用open(isa);也可用open()再connect(isa);的方式,但不能open()再bind(isa);因为bind是绑定本地ip;open(isa)方法说明:This convenience method works as if by invoking the open() method, invoking the connect method upon the resulting socket channel, passing it remote, and then returning that channel.  
不管服务端还是客户端都是select()->selectedKeys()->selectedKeys().remove(sk);然后判断sk的读取状态,服务端还要判断连接状态。

3. 还有一个问题如下代码在windows上运行多个客户端后关闭一个客户端是没有问题的,但在mac上却会使服务端无限输出,因为会不停地接收读取
事件,需要将try块中的代码改成如下,catch不变:  
int count = sc.read(buff);  
if(count >0){  
    buff.flip();  
    content = charset.decode(buff);  
    System.out.println("读取的数据:”+content);  
    sk.interestOps(SelectionKey.OP_READ);  
}else {  
    sc.close();  
}  
可查看下sc.close()、sk.cancel()、sk.channel().close()三者的区别 

4.server.register(selector, SelectionKey.OP_ACCEPT);
sc.register(selector, SelectionKey.OP_READ); 
即注册时使用的SelectionKey的值就是调用keys()或者selectedKeys()时返回的SelectionKey的类型,不可随便用,
即注册时用的读类型就不能写了

 

public class NServer {  
    // 用于检测所有Channel状态的Selector  
    private Selector selector = null;  
    static final int PORT = 30000;  
    // 定义实现编码、解码的字符集对象  
    private Charset charset = Charset.forName("UTF-8");  
  
    public static void main(String[] args) throws IOException {  
        new NServer().init();  
    }  
  
    public void init() throws IOException {  
        selector = Selector.open();  
        // 通过open方法来打开一个未绑定的ServerSocketChannel实例  
        ServerSocketChannel server = ServerSocketChannel.open();  
        InetSocketAddress isa = new InetSocketAddress("127.0.0.1", PORT);  
        // 将该ServerSocketChannel绑定到指定IP地址  
        server.bind(isa);  
        // 设置ServerSocket以非阻塞方式工作  
        server.configureBlocking(false);  
        // 将server注册到指定Selector对象  
        server.register(selector, SelectionKey.OP_ACCEPT);  
        while (selector.select() > 0) {  
            // 依次处理selector上的每个已选择的SelectionKey  
            for (SelectionKey sk : selector.selectedKeys()) {  
                // 从selector上的已选择Key集中删除正在处理的SelectionKey  
                selector.selectedKeys().remove(sk); // ①  
                // 如果sk对应的Channel包含客户端的连接请求  
                if (sk.isAcceptable()) // ②  
                {  
                    // 调用accept方法接受连接,产生服务器端的SocketChannel  
                    SocketChannel sc = server.accept();  
                    // 设置采用非阻塞模式  
                    sc.configureBlocking(false);  
                    // 将该SocketChannel也注册到selector  
                    sc.register(selector, SelectionKey.OP_READ);  
                    // 将sk对应的Channel设置成准备接受其他请求  
                    sk.interestOps(SelectionKey.OP_ACCEPT);  
                }  
                // 如果sk对应的Channel有数据需要读取  
                if (sk.isReadable()) // ③  
                {  
                    // 获取该SelectionKey对应的Channel,该Channel中有可读的数据  
                    SocketChannel sc = (SocketChannel) sk.channel();  
                    // 定义准备执行读取数据的ByteBuffer  
                    ByteBuffer buff = ByteBuffer.allocate(1024);  
                    String content = "";  
                    // 开始读取数据  
                    try {  
                        while (sc.read(buff) > 0) {  
                            buff.flip();  
                            content += charset.decode(buff);  
                        }  
                        // 打印从该sk对应的Channel里读取到的数据  
                        System.out.println("读取的数据:" + content);  
                        // 将sk对应的Channel设置成准备下一次读取  
                        sk.interestOps(SelectionKey.OP_READ);  
                    }  
                    // 如果捕捉到该sk对应的Channel出现了异常,即表明该Channel  
                    // 对应的Client出现了问题,所以从Selector中取消sk的注册  
                    catch (IOException ex) {  
                        // 从Selector中删除指定的SelectionKey  
                        sk.cancel();  
                        if (sk.channel() != null) {  
                            sk.channel().close();  
                        }  
                    }  
                    // 如果content的长度大于0,即聊天信息不为空  
                    if (content.length() > 0) {  
                        // 遍历该selector里注册的所有SelectionKey  
                        for (SelectionKey key : selector.keys()) {  
                            // 获取该key对应的Channel  
                            Channel targetChannel = key.channel();  
                            // 如果该channel是SocketChannel对象  
                            if (targetChannel instanceof SocketChannel) {  
                                // 将读到的内容写入该Channel中  
                                SocketChannel dest = (SocketChannel) targetChannel;  
                                dest.write(charset.encode(content));  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }  
  
}  
  
public class NClient {  
    // 定义检测SocketChannel的Selector对象  
    private Selector selector = null;  
    static final int PORT = 30000;  
    // 定义处理编码和解码的字符集  
    private Charset charset = Charset.forName("UTF-8");  
    // 客户端SocketChannel  
    private SocketChannel sc = null;  
  
    public static void main(String[] args) throws IOException {  
        new NClient().init();  
    }  
  
    public void init() throws IOException {  
        selector = Selector.open();  
        InetSocketAddress isa = new InetSocketAddress("127.0.0.1", PORT);  
        // 调用open静态方法创建连接到指定主机的SocketChannel  
        sc = SocketChannel.open(isa);  
        // 设置该sc以非阻塞方式工作  
        sc.configureBlocking(false);  
        // 将SocketChannel对象注册到指定Selector  
        sc.register(selector, SelectionKey.OP_READ);  
        // 启动读取服务器端数据的线程  
        new ClientThread().start();  
        // 创建键盘输入流  
        Scanner scan = new Scanner(System.in);  
        while (scan.hasNextLine()) {  
            // 读取键盘输入  
            String line = scan.nextLine();  
            // 将键盘输入的内容输出到SocketChannel中  
            sc.write(charset.encode(line));  
        }  
    }  
  
    // 定义读取服务器数据的线程  
    private class ClientThread extends Thread {  
        public void run() {  
            try {  
                while (selector.select() > 0) {  
                    // 遍历每个有可用IO操作Channel对应的SelectionKey  
                    for (SelectionKey sk : selector.selectedKeys()) {  
                        // 删除正在处理的SelectionKey  
                        selector.selectedKeys().remove(sk);  
                        // 如果该SelectionKey对应的Channel中有可读的数据  
                        if (sk.isReadable()) {  
                            // 使用NIO读取Channel中的数据  
                            SocketChannel sc = (SocketChannel) sk.channel();  
                            ByteBuffer buff = ByteBuffer.allocate(1024);  
                            String content = "";  
                            while (sc.read(buff) > 0) {  
                                // sc.read(buff);  
                                buff.flip();  
                                content += charset.decode(buff);  
                            }  
                            // 打印输出读取的内容  
                            System.out.println("聊天信息:" + content);  
                            // 为下一次读取作准备  
                            sk.interestOps(SelectionKey.OP_READ);  
                        }  
                    }  
                }  
            } catch (IOException ex) {  
                ex.printStackTrace();  
            }  
        }  
    }  
  
} 

 

 

 

 

 

posted @ 2017-04-05 22:36  john8169  阅读(250)  评论(0编辑  收藏  举报