public static void handleSelectionKey(SelectionKey selectionKey)
throws Exception {
if (selectionKey.isAcceptable()) { // 有客户端进来
clientCount++; ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey
.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket(); // 立即注册一个 OP_READ 的SelectionKey, 接收客户端的消息
SelectionKey key = socketChannel.register(selector,
SelectionKey.OP_READ);
key.attach("第 " + clientCount + " 个客户端 ["
+ socket.getRemoteSocketAddress() + "]: "); p(key.attachment()
+ "\t[接入] ========================================="); } else if (selectionKey.isReadable()) {  ByteBuffer byteBuffer=ByteBuffer.allocate(100);
             SocketChannel socketChannel=(SocketChannel)selectionKey.channel();
             
             try
             {
                 int len=socketChannel.read(byteBuffer);
                 String newMsg="";
                 // 如果len>0,表示有输入。如果len==0, 表示输入结束。需要关闭
                 // socketChannel
                 if (len>0)
                 {
                     
                     byteBuffer.flip();
                     String msg=charsetDecoder.decode(byteBuffer).toString();
                     
                     if (msg.substring(0, 3).equals(":5E")) {
      WriteIpPort.saveip(socketChannel.socket().getInetAddress()
      .toString(), socketChannel.socket().getPort(), msg
      .substring(3, msg.length()), 1);
      newMsg = "Start data transfer.";
      if(!checksocketbyselectkey(selectionKey,msg.substring(3, msg.length()),socketChannel.socket().getInetAddress()
      .toString(),socketChannel.socket().getPort())){
     
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.substring(3, msg.length()));
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
      //whilelist();
      }
      else
      {
      if(checksocketlist(msg.substring(3, msg.length()))){
     
      removelist(Getsocket(msg.substring(3, msg.length())));
     
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.substring(3, msg.length()));
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
     
      //whilelist();
      }
      else
      {
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.substring(3, msg.length()));
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
     
      //whilelist();
      }
      }
     
      }
      if (msg.substring(0, 3).equals(":50")) {
      Getdata.getdata1(msg);
      newMsg = "Stop data transfer.";
      if(!checksocketbyselectkey(selectionKey,msg.split(",")[8],socketChannel.socket().getInetAddress()
      .toString(),socketChannel.socket().getPort())){
     
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.split(",")[8]);
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
      //whilelist();
      }
      else
      {
      if(checksocketlist(msg.split(",")[8])){
     
      removelist(Getsocket(msg.split(",")[8]));
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.split(",")[8]);
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
     
      //whilelist();
      }
      else
      {
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.split(",")[8]);
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
     
      //whilelist();
      }
      }
      }
      if (msg.substring(0, 3).equals(":5F")) {
      Getdata.getdata1(msg);
      newMsg = "Stop data transfer.";
      if(!checksocketbyselectkey(selectionKey,msg.split(",")[8],socketChannel.socket().getInetAddress()
      .toString(),socketChannel.socket().getPort())){
     
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.split(",")[8]);
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
      //whilelist();
      }
      else
      {
      if(checksocketlist(msg.split(",")[8])){
     
      removelist(Getsocket(msg.split(",")[8]));
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.split(",")[8]);
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
     
      //whilelist();
      }
      else
      {
      userlist alist = new userlist();
      alist.setSelectionKey(selectionKey);
      alist.setTno(msg.split(",")[8]);
      alist.setSocketChannel(socketChannel);
      alist.setMyip(socketChannel.socket().getInetAddress()
      .toString());
      alist.setMyport(socketChannel.socket().getPort());
      list.add(alist);
     
      //whilelist();
      }
      }
     
      }                     if (newMsg==null)
                         newMsg="Sorry? I don't understand your message. ";
                     
                     // UTF-8 格式输出到客户端,并输出一个'n'
                     
                     socketChannel.write(charsetEncoder.encode(CharBuffer.wrap(newMsg
                             +"\n")));
                     p(selectionKey.attachment()
                             +"\t[recieved]: "
                             +msg
                             +" ----->\t[send]: "
                             +newMsg);
                     
                 } else
                 {
                     // 输入结束,关闭 socketChannel
                     p(selectionKey.attachment()
                             +"read finished. close socketChannel. ");
                     removelist(selectionKey);
                     socketChannel.close();
                     clientCount--; 
                 }
                 
             } catch (Exception e)
             {
                 
                 // 如果read抛出异常,表示连接异常中断,需要关闭
                 // socketChannel
                // e.printStackTrace();
                 removelist(selectionKey);
                 p(selectionKey.attachment()
                         +"第一个客户端关闭了 ");
                 socketChannel.close();
                 clientCount--;
             }
             
         } else
             if (selectionKey.isWritable())
             {
                 p(selectionKey.attachment()
                         +"TODO: isWritable() ???????????????????????????? ");
             } else
                 if (selectionKey.isConnectable())
                 {
                     p(selectionKey.attachment()
                             +"TODO: isConnectable() ????????????????????????? ");
                 } else
                 {
                     p(selectionKey.attachment()
                             +"TODO: else. ");
                 }
     
 }是这样的,客户端能够长连接,当客户端直接断电时,服务器不能检测到抛出的异常,就是服务器认为客户端还是连上的,并且要在4、5分钟后才能检测都客户单断开了,如果客户端是正常的断开,服务器马上就能检测到。这个问题怎么解决啊