uffer = CharsetHelper.decode(readBuffer); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CharBuffer charBuffer = Charset.forName("UTF-8").newDecoder().decode(readBuffer); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String question = charBuffer.toString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String answer = Helper.getAnswer(question); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /*//写入也是异步调用,也可以使用传入CompletionHandler对象的方式来处理写入结果 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //channel.write(CharsetHelper.encode(CharBuffer.wrap(answer)));? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? channel.write(Charset.forName("UTF-8").newEncoder().encode(CharBuffer.wrap(answer))); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //Unchecked exception thrown when an attempt is made to write to an asynchronous socket channel and a previous write has not completed. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //看来操作系统也不可靠 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? catch(WritePendingException wpe){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //休息一秒再重试,如果失败就不管了 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Helper.sleep(1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? channel.write(Charset.forName("UTF-8").newEncoder().encode(CharBuffer.wrap(answer))); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }*/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? writeStringMessage(channel, answer); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? readBuffer.clear(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? catch(IOException e){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果客户端关闭socket,那么服务器也需要关闭,否则浪费CPU ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? channel.close(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //异步调用OS处理下个读取请求 ? ? ? ? ? ? ? ? ? ? ? ? //这里传入this是个地雷,小心多线程 ? ? ? ? ? ? ? ? ? ? ? ? channel.read(readBuffer, null, this); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? /** ? ? ? ? ? ? ? ? ? ? * 服务器读失败处理 ? ? ? ? ? ? ? ? ? ? * @param exc ? ? ? ? ? ? ? ? ? ? * @param attachment ? ? ? ? ? ? ? ? ? ? */ ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void failed(Throwable exc, Object attachment) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("server read failed: " + exc);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(channel != null){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? channel.close(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? /** ? ? ? ? ? ? * 服务器接受连接失败处理 ? ? ? ? ? ? * @param exc ? ? ? ? ? ? * @param attachment ? ? ? ? ? ? */ ? ? ? ? ? ? @Override ? ? ? ? ? ? public void failed(Throwable exc, Object attachment) { ? ? ? ? ? ? ? ? System.out.println("server accept failed: " + exc); ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? }); ? ? } ? ? ? ? /** ? ? * Enqueues a write of the buffer to the channel. ? ? * The call is asynchronous so the buffer is not safe to modify after ? ? * passing the buffer here. ? ? * ? ? * @param buffer the buffer to send to the channel ? ? */ ? ? private void writeMessage(final AsynchronousSocketChannel channel, final ByteBuffer buffer) { ? ? ? ? boolean threadShouldWrite = false; ? ? ? ? ? synchronized(queue) { ? ? ? ? ? ? queue.add(buffer); ? ? ? ? ? ? // Currently no thread writing, make this thread dispatch a write ? ? ? ? ? ? if (!writing) { ? ? ? ? ? ? ? ? writing = true; ? ? ? ? ? ? ? ? threadShouldWrite = true; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? if (threadShouldWrite) { ? ? ? ? ? ? writeFromQueue(channel); ? ? ? ? } ? ? } ? ? ? private void writeFromQueue(final AsynchronousSocketChannel channel) { ? ? ? ? ByteBuffer buffer; ? ? ? ? ? synchronized (queue) { ? ? ? ? ? ? buffer = queue.poll(); ? ? ? ? ? ? if (buffer == null) { ? ? ? ? ? ? ? ? writing = false; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? // No new data in buffer to write ? ? ? ? if (writing) { ? ? ? ? ? ? writeBuffe |