client端:
1 SocketChannel.open() 打开SocketChannel
2 SocketChannel.configureBlocking(false) 将SocketChannel配置为非阻塞模式
3 SocketChannel.connect(host,port) 连接到指定的目标地址
4 Selector.open() 打开Selector
5 SocketChannel.register(Selector,int) 向Selector注册感兴趣的事件,connected,read,write
6 while(true) 循环执行保证客户端一直处于运行状态
7 Selector.select() 从Selector中获取是否有可读的key信息
8 for(SelectionKey key:selector.selectedKeys()) 遍历selector中所有selectedKeys
9 SelectionKey.isConnectable() 判断是否为连接建立的类型
10 SelectionKey.channel() 获取绑定的SocketChannel
11 SocketChannel.finishConnect() 完成连接的建立(TCP/IP的三次握手)
12 SelectionKey.isReadable() 判断是否为可读类型
13 SelectionKey.channel() 获取绑定的SocketChannel
14 SocketChannel.read(ByteBuffer) 从SocketChannel中读取数到ByteBuffer中
15 SocketChannel.write(ByteBuffer) 向SocketChannel中写入ByteBuffer对象数据