|
| 1 | +package com.prd.io.nio; |
| 2 | + |
| 3 | +import com.prd.io.bio.BIOChatClient; |
| 4 | + |
| 5 | +import java.io.Closeable; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.channels.SelectionKey; |
| 10 | +import java.nio.channels.Selector; |
| 11 | +import java.nio.channels.ServerSocketChannel; |
| 12 | +import java.nio.channels.SocketChannel; |
| 13 | +import java.nio.charset.Charset; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +/** |
| 17 | + * NIO实现多人聊天室,功能 |
| 18 | + * (1)基于NIO模型 |
| 19 | + * (2)支持多人同时在线 |
| 20 | + * (3)每个用户的发言都被转发给其他用户 |
| 21 | + * |
| 22 | + * NIO客户端 非阻塞 |
| 23 | + */ |
| 24 | +public class NIOChatClient { |
| 25 | + |
| 26 | + public static final int DEFAULT_PORT = 8888; |
| 27 | + public static final String DEFAULT_HOST="127.0.0.1"; |
| 28 | + public static final String QUIT ="quit"; |
| 29 | + public static final int BUFFER = 1024; |
| 30 | + |
| 31 | + private SocketChannel client; |
| 32 | + private Selector selector; |
| 33 | + private ByteBuffer rBuffer = ByteBuffer.allocate(BUFFER); |
| 34 | + private ByteBuffer wBuffer = ByteBuffer.allocate(BUFFER); |
| 35 | + private Charset charset = Charset.forName("UTF-8"); |
| 36 | + private int port; |
| 37 | + private String host; |
| 38 | + |
| 39 | + public NIOChatClient(int port, String host) { |
| 40 | + this.port = port; |
| 41 | + this.host = host; |
| 42 | + } |
| 43 | + |
| 44 | + public NIOChatClient() { |
| 45 | + this(DEFAULT_PORT,DEFAULT_HOST); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * 检查用户是否退出 |
| 50 | + * @param msg |
| 51 | + * @return |
| 52 | + */ |
| 53 | + public boolean readyToQuit(String msg) { |
| 54 | + return QUIT.equals(msg); |
| 55 | + } |
| 56 | + |
| 57 | + public void close(Closeable closeable) { |
| 58 | + if (closeable!=null) { |
| 59 | + try { |
| 60 | + closeable.close(); |
| 61 | + } catch (IOException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 启动客户端 |
| 69 | + */ |
| 70 | + public void start() { |
| 71 | + try { |
| 72 | + client = SocketChannel.open(); |
| 73 | + client.configureBlocking(false); |
| 74 | + |
| 75 | + selector = Selector.open(); |
| 76 | + //注册 连接事件监听 |
| 77 | + client.register(selector,SelectionKey.OP_CONNECT); |
| 78 | + client.connect(new InetSocketAddress(host,port)); |
| 79 | + |
| 80 | + while(true) { |
| 81 | + selector.select(); |
| 82 | + Set<SelectionKey> selectionKeys = selector.selectedKeys(); |
| 83 | + for (SelectionKey key:selectionKeys) { |
| 84 | + handlers(key); |
| 85 | + } |
| 86 | + selectionKeys.clear(); |
| 87 | + } |
| 88 | + |
| 89 | + } catch (IOException e) { |
| 90 | + e.printStackTrace(); |
| 91 | + } finally { |
| 92 | + // 只关闭selector,就可以同时关闭selector上注册的通道 |
| 93 | + close(selector); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void handlers(SelectionKey key) throws IOException { |
| 98 | + SocketChannel client = (SocketChannel) key.channel(); |
| 99 | + // CONNECT事件 --连接就绪事件 |
| 100 | + if (key.isConnectable()) { |
| 101 | + // 判断连接是否就绪,如果返回false表明连接正在进行,需要等待。 |
| 102 | + if (client.isConnectionPending()) { |
| 103 | + client.finishConnect(); |
| 104 | + // 处理用户的输入 |
| 105 | +// new Thread(new UserInputHandler(this)).start(); |
| 106 | + } |
| 107 | + client.register(selector,SelectionKey.OP_READ); |
| 108 | + } |
| 109 | + // READ事件 --服务器转发消息 |
| 110 | + else if (key.isReadable()) { |
| 111 | + String msg = receive(client); |
| 112 | + if (msg.isEmpty()) { |
| 113 | + //服务器异常,则客户端退出 |
| 114 | + close(selector); |
| 115 | + } else { |
| 116 | + System.out.println(msg); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private String receive(SocketChannel client) throws IOException { |
| 122 | + rBuffer.clear(); |
| 123 | + while (client.read(rBuffer)>0); |
| 124 | + rBuffer.flip(); |
| 125 | + return String.valueOf(charset.decode(rBuffer)); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + private static class UserInputHandler implements Runnable{ |
| 130 | + |
| 131 | + //TODO 未完成 |
| 132 | +// private NIOChatClient c |
| 133 | + |
| 134 | + @Override |
| 135 | + public void run() { |
| 136 | + |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments