设为首页 加入收藏

TOP

Java AIO 服务器与客户端实现示例(二)
2015-02-02 14:44:13 来源: 作者: 【 】 浏览:78
Tags:Java AIO 服务器 客户端 实现 示例
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
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java NIO操作类型 下一篇Java NIO 服务器与客户端实现示例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: