|  
 
 
当多个终端连接到服务器后,将它们都注册到服务器的一个Hashtable中: 
Hashtable<String,SocketChannel> clientlist=new Hashtable<String,SocketChannel>(); String 相当于终端的用户名,SocketChannel 是各个终端的通道。 
现在,如果一个终端关闭,服务器捕获这个异常后,如何确定是哪个终端呢?   if (key.isReadable()) {                         int count = 0;                            SocketChannel channel = (SocketChannel) key.channel();                            buffer.clear();                            while ((count = channel.read(buffer)) > 0) {                                buffer.flip();                                //while (buffer.hasRemaining()) {                                 String receiveText = String.valueOf(cs.decode(buffer).array());                                    System.out.println(receiveText);                                    dispatch(channel, receiveText);                                  //}                                    channel.register(selector, SelectionKey.OP_READ);                             }                            if (count < 0) {                                channel.close();                                System.out.println("断开。。。"+PORT_NUMBER);                            }                     }                 } 我是这样写的,判断是哪个终端断开的  register(Selector sel, int ops, Object att) 的时候 在 Object att 里面存 终端的用户名 异常的时候,通过 SelectionKey.attachment(),把 终端的用户名 取出来 
 |