最近在学Java的NIO编程,学习过程中编写了一个Demo,特此记录。
- 代码目录结构:
-
代码描述: 使用ThreadPool启动十个客户端线程模拟十个请求,并与服务端保持长连接。每个客户端每隔3秒向服务端发送数据,服务端监听客户端的连接和可读事件,并输出客户端内容。半分钟过后线程池强制中断客户端线程,客户端关闭与服务端的连接,服务端监听客户端的关闭,并将相应事件从selector中取消。
- 客户端代码内容:
NioClient.java 主线程,启动客户端程序。
1 package com.vincent.fat.core.nio.client; 2 3 4 import java.util.concurrent.ExecutorService; 5 import java.util.concurrent.Executors; 6 import java.util.concurrent.TimeUnit; 7 import java.util.concurrent.atomic.AtomicLong; 8 9 public class NioClient { 10 private static final ExecutorService executor = Executors.newCachedThreadPool(); 11 private static final AtomicLong countSend=new AtomicLong(0L); 12 public static void main(String[] args) throws InterruptedException { 13 clientStart(); 14 } 15 public static void clientStart() throws InterruptedException { 16 OnlyNosClients(); 17 } 18 19 private static void OnlyNosClients() throws InterruptedException { 20 for (int i = 0; i < 10; i++) { 21 executor.submit( 22 new RequestRemoteServerWithTcp("localhost",17899,countSend) 23 ); 24 } 25 //10秒钟后中断所有线程 26 TimeUnit.SECONDS.sleep(10); 27 executor.shutdownNow(); 28 } 29 30 private static void startNewClients() { 31 while (!Thread.currentThread().isInterrupted()) 32 { 33 executor.submit( 34 new RequestRemoteServerWithTcp("localhost",17899,countSend) 35 ); 36 } 37 } 38 }
RequestRemoteServerWithTcp.java 客户端启动代码
其中 com.github.javafaker.Faker 是用来生成随机数据的类,org.slf4j.Logger 是用于日志输出的类
1 package com.vincent.fat.core.nio.client; 2 3 import com.github.javafaker.Faker; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 7 import java.io.IOException; 8 import java.net.InetSocketAddress; 9 import java.net.SocketAddress; 10 import java.nio.ByteBuffer; 11 import java.nio.channels.SocketChannel; 12 import java.nio.charset.StandardCharsets; 13 import java.util.Locale; 14 import java.util.Objects; 15 import java.util.concurrent.TimeUnit; 16 import java.util.concurrent.atomic.AtomicLong; 17 18 public class RequestRemoteServerWithTcp implements Runnable { 19 public static final Logger logger = LoggerFactory.getLogger(RequestRemoteServerWithTcp.class); 20 public static final Locale locale=Locale.SIMPLIFIED_CHINESE; 21 public static final Faker faker =new Faker(locale); 22 private final String serverAddress; 23 private final int serverPoint; 24 private int localPoint; 25 private final AtomicLong countSend ; 26 public RequestRemoteServerWithTcp(String serverAddress,int serverPoint,AtomicLong countSend){ 27 this.serverAddress=serverAddress; 28 this.serverPoint=serverPoint; 29 this.countSend=countSend; 30 } 31 /** 32 * When an object implementing interface <code>Runnable</code> is used 33 * to create a thread, starting the thread causes the object's 34 * <code>run</code> method to be called in that separately executing 35 * thread. 36 * <p> 37 * The general contract of the method <code>run</code> is that it may 38 * take any action whatsoever. 39 * 40 * @see Thread#run() 41 */ 42 @Override 43 public void run() { 44 try { 45 this.doRequest(); 46 } catch (IOException ioException) { 47 ioException.printStackTrace(); 48 } 49 } 50 private void doRequest() throws IOException { 51 SocketChannel socketChannel = null; 52 try { 53 socketChannel = SocketChannel.open(new InetSocketAddress(this.serverAddress,this.serverPoint)); 54 socketChannel.configureBlocking(false); 55 SocketAddress localAddress = socketChannel.getLocalAddress(); 56 while(!socketChannel.isConnected()){} 57 logger.debug("[{}]完成连接",socketChannel.getLocalAddress()); 58 this.localPoint = ((InetSocketAddress) socketChannel.getLocalAddress()).getPort(); 59 while (!Thread.currentThread().isInterrupted()) 60 { 61 ByteBuffer buffer = this.prepareData(this.localPoint); 62 socketChannel.write(buffer); 63 buffer.clear(); 64 long currentSendCount = this.countSend.incrementAndGet(); 65 logger.info("客户端[{}]的第【{}】条数据发送完成",localAddress,currentSendCount); 66 TimeUnit.SECONDS.sleep(3); 67 ByteBuffer readBuffer = ByteBuffer.allocate(2*1024); 68 getResponseData(socketChannel, readBuffer); 69 } 70 } catch (IOException ioException) { 71 logger.error("ioException:",ioException); 72 }catch (Exception e) 73 { 74 logger.error("端口为{}的客户端报错",this.localPoint,e); 75 }finally { 76 if (!Objects.isNull(socketChannel)&&socketChannel.isOpen()) 77 { 78 //关闭输出通道 79 socketChannel.shutdownOutput(); 80 socketChannel.shutdownInput(); 81 socketChannel.close(); 82 logger.info("【{}】的输出通道关闭!",this.localPoint); 83 } 84 } 85 86 } 87 88 private void getResponseData(SocketChannel socketChannel, ByteBuffer readBuffer) throws IOException { 89 //接收客户端数据 90 int readBytes = 0; 91 StringBuilder content = new StringBuilder(); 92 while ((readBytes=socketChannel.read(readBuffer))>0) { 93 //处理数据 94 content.append(new String(readBuffer.array(), StandardCharsets.UTF_8)); 95 readBuffer.clear(); 96 } 97 //判断服务端断开连接 98 //当服务端没有断开连接的时候readBytes=0这样保持长连接 99 //当服务端断开连接的时候readBytes=-1 100 if (readBytes<0) 101 { 102 logger.debug("服务端断开连接....."); 103 socketChannel.close(); 104 } 105 //处理数据 106 else { 107 logger.info("收到的内容是: <{}>", 108 content.toString()); 109 } 110 } 111 112 private ByteBuffer prepareData(int channelPoint) { 113 StringBuilder name =new StringBuilder(faker.university().name()); 114 name.insert(0,(channelPoint+"==========>")); 115 return ByteBuffer.wrap(name.toString().getBytes(StandardCharsets.UTF_8)); 116 } 117 }
服务端代码:NioServer.java
1 package com.vincent.fat.core.nio.server; 2 3 import cn.hutool.core.util.RandomUtil; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 7 import java.io.IOException; 8 import java.net.InetSocketAddress; 9 import java.nio.ByteBuffer; 10 import java.nio.channels.*; 11 import java.nio.charset.StandardCharsets; 12 import java.util.Iterator; 13 import java.util.Objects; 14 import java.util.Set; 15 import java.util.concurrent.*; 16 import java.util.concurrent.atomic.AtomicInteger; 17 18 public class NioServer { 19 public static final Logger logger = LoggerFactory.getLogger(NioServer.class); 20 private static final Long TIME_OUT= 3000L; 21 private static final BlockingQueue<Future<String>> futureQueue =new LinkedBlockingQueue<>(); 22 private static final AtomicInteger selectorNos=new AtomicInteger(0); 23 private static final AtomicInteger handleDataNo=new AtomicInteger(0); 24 private static final AtomicInteger resDataNo=new AtomicInteger(0); 25 public static void main(String[] args) throws IOException, InterruptedException { 26 startServer(args); 27 } 28 public static void startServer(String[] args) throws IOException, InterruptedException { 29 //新建channel 30 ServerSocketChannel serverSocketChannel =ServerSocketChannel.open(); 31 //设置服务端属性,为非阻塞模式 32 serverSocketChannel.configureBlocking(false); 33 //绑定服务端端口 34 serverSocketChannel.bind(new InetSocketAddress(17899)); 35 //新建selector 36 Selector selector = Selector.open(); 37 //注册channel到selector 38 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); 39 logger.debug("服务在[{}]启动成功,正在监听事件。。。。。",serverSocketChannel.getLocalAddress()); 40 //while循环监听事件 41 while (!Thread.currentThread().isInterrupted()) 42 { 43 if (selector.select(TIME_OUT)>0) 44 { 45 logger.warn("第【{}】轮监听===============================================", 46 selectorNos.incrementAndGet()); 47 //获取事件类型 48 Set<SelectionKey> selectionKeys = selector.selectedKeys(); 49 //selector监听到的ready事件集 50 Iterator<SelectionKey> readyKeys = selectionKeys.iterator(); 51 //处理事件 52 while (readyKeys.hasNext()) 53 { 54 SelectionKey key = readyKeys.next(); 55 //移除channel事件 56 readyKeys.remove(); 57 SocketChannel socketChannel =null; 58 try { 59 60 //连接 61 if (key.isValid()&&key.isAcceptable()) { 62 socketChannel = ((ServerSocketChannel) key.channel()).accept(); 63 socketChannel.configureBlocking(false); 64 logger.info("Accept 收到了来自于【{}】的连接请求", socketChannel.getRemoteAddress()); 65 //将客户端的可读事件注册到selector中监听 66 socketChannel.register(key.selector(), SelectionKey.OP_READ); 67 } 68 //数据可读 69 if (key.isValid()&&key.isReadable()) { 70 socketChannel = (SocketChannel)key.channel(); 71 logger.info("Readable 客户端【{}】准备好了数据 ",socketChannel.getRemoteAddress()); 72 getRequestData(key); 73 } 74 if (key.isValid()&&key.isWritable()) 75 { 76 // if (RandomUtil.randomInt(0,10)>8) 77 // { 78 // doResponse(key); 79 // } 80 } 81 }catch (IOException ioException) 82 { 83 logger.error("服务端IO异常: ",ioException.getCause()); 84 if (!Objects.isNull(socketChannel)) 85 { 86 socketChannel.close(); 87 key.cancel(); 88 } 89 } 90 } 91 } 92 } 93 //关闭channel 94 serverSocketChannel.close(); 95 selector.close(); 96 logger.info("进程退出"); 97 } 98 //处理连接请求 99 private static void getRequestData(SelectionKey selectionKey) throws IOException { 100 SocketChannel socketChannel = (SocketChannel)selectionKey.channel(); 101 //写入数据到buffer 102 try { 103 if (socketChannel.isOpen()) 104 { 105 //将写事件注册到selector 106 // socketChannel.register(selectionKey.selector(), selectionKey.interestOps()|SelectionKey.OP_WRITE); 107 //准备buffer 108 ByteBuffer byteBuffer = ByteBuffer.allocate(2048); 109 StringBuilder content = new StringBuilder(); 110 int readBytes = 0; 111 while ((readBytes=socketChannel.read(byteBuffer))>0) { 112 //处理数据 113 content.append(new String(byteBuffer.array(), StandardCharsets.UTF_8)); 114 byteBuffer.clear(); 115 } 116 //判断客户端断开连接 117 //当客户端没有断开连接的时候readBytes=0这样保持长连接 118 //当客户端断开连接的时候readBytes=-1 119 if (readBytes<0) 120 { 121 logger.debug("远程客户端【{}】断开连接.....",socketChannel.getRemoteAddress()); 122 socketChannel.close(); 123 selectionKey.cancel(); 124 } 125 //处理数据 126 else { 127 logger.info("第【{}】次处理数据,收到的内容是: {}",handleDataNo.incrementAndGet(), 128 content.toString()); 129 } 130 131 } 132 else { 133 logger.warn("channel 已经关闭!"); 134 socketChannel.shutdownInput(); 135 socketChannel.shutdownOutput(); 136 socketChannel.close(); 137 selectionKey.cancel(); 138 } 139 }catch (Exception e) 140 { 141 logger.error("客户端【{}】错误:",socketChannel,e.getCause()); 142 //关闭连接 143 socketChannel.shutdownInput(); 144 socketChannel.shutdownOutput(); 145 //将channel从selector中删除 146 selectionKey.cancel(); 147 logger.info("socketChannel【{}】已经从selector取消注册",socketChannel.getRemoteAddress()); 148 socketChannel.close(); 149 } 150 } 151 //返回响应数据 152 private static void doResponse(SelectionKey selectionKey) throws IOException { 153 SocketChannel socketChannel = (SocketChannel)selectionKey.channel(); 154 String resData=resDataNo.incrementAndGet()+"=========>"+ RandomUtil.randomStringUpper(10); 155 logger.debug("响应数据[{}]到【{}】",resData,socketChannel.getRemoteAddress()); 156 socketChannel.write(ByteBuffer.wrap(resData.getBytes())); 157 } 158 }
先后启动服务端和客户端,运行结果:
服务端运行日志:
11:01:16.817 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 服务在[/0:0:0:0:0:0:0:0:17899]启动成功,正在监听事件。。。。。 11:01:19.840 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【1】轮监听=============================================== 11:01:19.841 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63958】的连接请求 11:01:19.841 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【2】轮监听=============================================== 11:01:19.841 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63959】的连接请求 11:01:19.842 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【3】轮监听=============================================== 11:01:19.843 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63960】的连接请求 11:01:19.843 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【4】轮监听=============================================== 11:01:19.843 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63961】的连接请求 11:01:19.847 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【5】轮监听=============================================== 11:01:19.847 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63962】的连接请求 11:01:19.848 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【6】轮监听=============================================== 11:01:19.848 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63963】的连接请求 11:01:19.850 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【7】轮监听=============================================== 11:01:19.850 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63964】的连接请求 11:01:19.851 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【8】轮监听=============================================== 11:01:19.851 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63965】的连接请求 11:01:19.853 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【9】轮监听=============================================== 11:01:19.853 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63966】的连接请求 11:01:19.853 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【10】轮监听=============================================== 11:01:19.854 [main] INFO com.vincent.fat.core.nio.server.NioServer - Accept 收到了来自于【/127.0.0.1:63967】的连接请求 11:01:20.091 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【11】轮监听=============================================== 11:01:20.091 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63962】准备好了数据 11:01:20.092 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【1】次处理数据,收到的内容是: 63962==========>西南科技大学 11:01:20.092 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【12】轮监听=============================================== 11:01:20.092 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63964】准备好了数据 11:01:20.092 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【2】次处理数据,收到的内容是: 63964==========>西北技术大学 11:01:20.094 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【13】轮监听=============================================== 11:01:20.095 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63961】准备好了数据 11:01:20.095 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【3】次处理数据,收到的内容是: 63961==========>西科技大学 11:01:20.095 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【14】轮监听=============================================== 11:01:20.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63958】准备好了数据 11:01:20.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【4】次处理数据,收到的内容是: 63958==========>东大学 11:01:20.096 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【15】轮监听=============================================== 11:01:20.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63959】准备好了数据 11:01:20.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【5】次处理数据,收到的内容是: 63959==========>西北科技大学 11:01:20.100 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【16】轮监听=============================================== 11:01:20.100 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63960】准备好了数据 11:01:20.101 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【6】次处理数据,收到的内容是: 63960==========>西南大学 11:01:20.102 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【17】轮监听=============================================== 11:01:20.102 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63965】准备好了数据 11:01:20.102 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【7】次处理数据,收到的内容是: 63965==========>西南科技大学 11:01:20.102 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【18】轮监听=============================================== 11:01:20.102 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63967】准备好了数据 11:01:20.102 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【8】次处理数据,收到的内容是: 63967==========>东科技大学 11:01:20.103 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【19】轮监听=============================================== 11:01:20.103 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63963】准备好了数据 11:01:20.103 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【9】次处理数据,收到的内容是: 63963==========>西南体育大学 11:01:20.105 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【20】轮监听=============================================== 11:01:20.105 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63966】准备好了数据 11:01:20.105 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【10】次处理数据,收到的内容是: 63966==========>北理工大学 11:01:23.093 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【21】轮监听=============================================== 11:01:23.093 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63962】准备好了数据 11:01:23.093 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【11】次处理数据,收到的内容是: 63962==========>中国艺术大学 11:01:23.094 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【22】轮监听=============================================== 11:01:23.094 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63964】准备好了数据 11:01:23.095 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【12】次处理数据,收到的内容是: 63964==========>北艺术大学 11:01:23.095 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【23】轮监听=============================================== 11:01:23.095 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63961】准备好了数据 11:01:23.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【13】次处理数据,收到的内容是: 63961==========>北体育大学 11:01:23.096 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【24】轮监听=============================================== 11:01:23.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63958】准备好了数据 11:01:23.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【14】次处理数据,收到的内容是: 63958==========>南农业大学 11:01:23.098 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【25】轮监听=============================================== 11:01:23.098 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63959】准备好了数据 11:01:23.098 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【15】次处理数据,收到的内容是: 63959==========>西艺术大学 11:01:23.103 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【26】轮监听=============================================== 11:01:23.103 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63960】准备好了数据 11:01:23.103 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【16】次处理数据,收到的内容是: 63960==========>南科技大学 11:01:23.103 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【27】轮监听=============================================== 11:01:23.103 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63967】准备好了数据 11:01:23.103 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【17】次处理数据,收到的内容是: 63967==========>南农业大学 11:01:23.104 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【28】轮监听=============================================== 11:01:23.104 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63965】准备好了数据 11:01:23.104 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【18】次处理数据,收到的内容是: 63965==========>西北技术大学 11:01:23.105 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【29】轮监听=============================================== 11:01:23.105 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63963】准备好了数据 11:01:23.105 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【19】次处理数据,收到的内容是: 63963==========>西科技大学 11:01:23.106 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【30】轮监听=============================================== 11:01:23.106 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63966】准备好了数据 11:01:23.106 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【20】次处理数据,收到的内容是: 63966==========>西经贸大学 11:01:26.094 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【31】轮监听=============================================== 11:01:26.095 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63962】准备好了数据 11:01:26.095 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【21】次处理数据,收到的内容是: 63962==========>东南科技大学 11:01:26.096 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【32】轮监听=============================================== 11:01:26.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63964】准备好了数据 11:01:26.097 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【22】次处理数据,收到的内容是: 63964==========>西南艺术大学 11:01:26.097 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【33】轮监听=============================================== 11:01:26.097 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63961】准备好了数据 11:01:26.097 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【23】次处理数据,收到的内容是: 63961==========>东南体育大学 11:01:26.098 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【34】轮监听=============================================== 11:01:26.098 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63958】准备好了数据 11:01:26.098 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【24】次处理数据,收到的内容是: 63958==========>南艺术大学 11:01:26.099 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【35】轮监听=============================================== 11:01:26.100 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63959】准备好了数据 11:01:26.100 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【25】次处理数据,收到的内容是: 63959==========>中国技术大学 11:01:26.104 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【36】轮监听=============================================== 11:01:26.104 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63967】准备好了数据 11:01:26.104 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【26】次处理数据,收到的内容是: 63967==========>北科技大学 11:01:26.105 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【37】轮监听=============================================== 11:01:26.105 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63960】准备好了数据 11:01:26.105 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【27】次处理数据,收到的内容是: 63960==========>东北理工大学 11:01:26.106 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【38】轮监听=============================================== 11:01:26.106 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63965】准备好了数据 11:01:26.106 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【28】次处理数据,收到的内容是: 63965==========>西科技大学 11:01:26.107 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【39】轮监听=============================================== 11:01:26.107 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63963】准备好了数据 11:01:26.107 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【29】次处理数据,收到的内容是: 63963==========>东北体育大学 11:01:26.110 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【40】轮监听=============================================== 11:01:26.110 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63966】准备好了数据 11:01:26.110 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【30】次处理数据,收到的内容是: 63966==========>中国大学 11:01:29.096 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【41】轮监听=============================================== 11:01:29.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63962】准备好了数据 11:01:29.096 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【31】次处理数据,收到的内容是: 63962==========>东北农业大学 11:01:29.098 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【42】轮监听=============================================== 11:01:29.098 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63964】准备好了数据 11:01:29.099 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【32】次处理数据,收到的内容是: 63964==========>西北农业大学 11:01:29.099 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【43】轮监听=============================================== 11:01:29.099 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63958】准备好了数据 11:01:29.100 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【33】次处理数据,收到的内容是: 63958==========>南体育大学 11:01:29.100 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【44】轮监听=============================================== 11:01:29.100 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63961】准备好了数据 11:01:29.100 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【34】次处理数据,收到的内容是: 63961==========>北大学 11:01:29.101 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【45】轮监听=============================================== 11:01:29.102 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63959】准备好了数据 11:01:29.102 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【35】次处理数据,收到的内容是: 63959==========>东农业大学 11:01:29.106 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【46】轮监听=============================================== 11:01:29.106 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63960】准备好了数据 11:01:29.107 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【36】次处理数据,收到的内容是: 63960==========>南大学 11:01:29.107 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【47】轮监听=============================================== 11:01:29.107 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63967】准备好了数据 11:01:29.107 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【37】次处理数据,收到的内容是: 63967==========>中国大学 11:01:29.108 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【48】轮监听=============================================== 11:01:29.108 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63965】准备好了数据 11:01:29.108 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【38】次处理数据,收到的内容是: 63965==========>西科技大学 11:01:29.109 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【49】轮监听=============================================== 11:01:29.110 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63963】准备好了数据 11:01:29.110 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【39】次处理数据,收到的内容是: 63963==========>南科技大学 11:01:29.112 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【50】轮监听=============================================== 11:01:29.112 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63966】准备好了数据 11:01:29.112 [main] INFO com.vincent.fat.core.nio.server.NioServer - 第【40】次处理数据,收到的内容是: 63966==========>西北科技大学 11:01:29.825 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【51】轮监听=============================================== 11:01:29.825 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63965】准备好了数据 11:01:29.825 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63965】断开连接..... 11:01:29.826 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【52】轮监听=============================================== 11:01:29.826 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63962】准备好了数据 11:01:29.826 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63962】断开连接..... 11:01:29.827 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【53】轮监听=============================================== 11:01:29.827 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63961】准备好了数据 11:01:29.827 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63961】断开连接..... 11:01:29.828 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【54】轮监听=============================================== 11:01:29.828 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63958】准备好了数据 11:01:29.828 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63958】断开连接..... 11:01:29.831 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【55】轮监听=============================================== 11:01:29.831 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63966】准备好了数据 11:01:29.831 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63966】断开连接..... 11:01:29.831 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【56】轮监听=============================================== 11:01:29.832 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63959】准备好了数据 11:01:29.832 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63959】断开连接..... 11:01:29.833 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【57】轮监听=============================================== 11:01:29.833 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63963】准备好了数据 11:01:29.833 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63963】断开连接..... 11:01:29.844 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【58】轮监听=============================================== 11:01:29.844 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63960】准备好了数据 11:01:29.844 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63960】断开连接..... 11:01:29.844 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【59】轮监听=============================================== 11:01:29.845 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63964】准备好了数据 11:01:29.845 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63964】断开连接..... 11:01:29.846 [main] WARN com.vincent.fat.core.nio.server.NioServer - 第【60】轮监听=============================================== 11:01:29.846 [main] INFO com.vincent.fat.core.nio.server.NioServer - Readable 客户端【/127.0.0.1:63967】准备好了数据 11:01:29.846 [main] DEBUG com.vincent.fat.core.nio.server.NioServer - 远程客户端【/127.0.0.1:63967】断开连接.....
客户端日志:
"C:Program FilesJavajdk1.8.0_221injava.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:63949,suspend=y,server=n -javaagent:C:Userswb-swz696586AppDataLocalJetBrainsIntelliJIdea2020.1captureAgentdebugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:softsIntelliJ IDEA 2019.3.4libidea_rt.jar" com.intellij.rt.execution.CommandLineWrapper C:Userswb-swz696586AppDataLocalTempidea_classpath1497081722 com.vincent.fat.core.nio.client.NioClient Connected to the target VM, address: '127.0.0.1:63949', transport: 'socket' 11:01:19.846 [pool-1-thread-7] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63958]完成连接 11:01:19.851 [pool-1-thread-2] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63965]完成连接 11:01:19.849 [pool-1-thread-4] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63959]完成连接 11:01:19.847 [pool-1-thread-6] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63962]完成连接 11:01:19.848 [pool-1-thread-5] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63960]完成连接 11:01:19.854 [pool-1-thread-3] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63966]完成连接 11:01:19.848 [pool-1-thread-10] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63961]完成连接 11:01:19.850 [pool-1-thread-1] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63964]完成连接 11:01:19.848 [pool-1-thread-8] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63963]完成连接 11:01:19.853 [pool-1-thread-9] DEBUG com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - [/127.0.0.1:63967]完成连接 11:01:20.092 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63962]的第【1】条数据发送完成 11:01:20.092 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63964]的第【2】条数据发送完成 11:01:20.095 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63961]的第【3】条数据发送完成 11:01:20.095 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63958]的第【4】条数据发送完成 11:01:20.097 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63959]的第【5】条数据发送完成 11:01:20.100 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63960]的第【6】条数据发送完成 11:01:20.101 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63967]的第【7】条数据发送完成 11:01:20.103 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63963]的第【9】条数据发送完成 11:01:20.102 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63965]的第【8】条数据发送完成 11:01:20.105 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63966]的第【10】条数据发送完成 11:01:23.092 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.093 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63962]的第【11】条数据发送完成 11:01:23.094 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.094 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63964]的第【12】条数据发送完成 11:01:23.095 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.095 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63961]的第【13】条数据发送完成 11:01:23.096 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.096 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63958]的第【14】条数据发送完成 11:01:23.097 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.098 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63959]的第【15】条数据发送完成 11:01:23.101 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.102 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.103 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63960]的第【16】条数据发送完成 11:01:23.103 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63967]的第【17】条数据发送完成 11:01:23.104 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.104 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.104 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63963]的第【18】条数据发送完成 11:01:23.104 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63965]的第【19】条数据发送完成 11:01:23.106 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:23.106 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63966]的第【20】条数据发送完成 11:01:26.094 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.094 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63962]的第【21】条数据发送完成 11:01:26.096 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.096 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63964]的第【22】条数据发送完成 11:01:26.097 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.097 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.097 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63961]的第【23】条数据发送完成 11:01:26.097 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63958]的第【24】条数据发送完成 11:01:26.099 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.099 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63959]的第【25】条数据发送完成 11:01:26.104 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.104 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.104 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63960]的第【26】条数据发送完成 11:01:26.104 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63967]的第【27】条数据发送完成 11:01:26.105 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.105 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.106 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63965]的第【28】条数据发送完成 11:01:26.107 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63963]的第【29】条数据发送完成 11:01:26.107 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:26.110 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63966]的第【30】条数据发送完成 11:01:29.095 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.096 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63962]的第【31】条数据发送完成 11:01:29.097 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.098 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.098 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.098 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63964]的第【32】条数据发送完成 11:01:29.099 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63958]的第【33】条数据发送完成 11:01:29.100 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63961]的第【34】条数据发送完成 11:01:29.101 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.101 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63959]的第【35】条数据发送完成 11:01:29.105 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.105 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.106 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63967]的第【36】条数据发送完成 11:01:29.106 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63960]的第【37】条数据发送完成 11:01:29.107 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.108 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63965]的第【38】条数据发送完成 11:01:29.108 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.109 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63963]的第【39】条数据发送完成 11:01:29.111 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 收到的内容是: <> 11:01:29.112 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 客户端[/127.0.0.1:63966]的第【40】条数据发送完成 11:01:29.823 [pool-1-thread-2] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63965的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.824 [pool-1-thread-6] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63962的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.826 [pool-1-thread-6] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63962】的输出通道关闭! 11:01:29.826 [pool-1-thread-10] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63961的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.827 [pool-1-thread-10] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63961】的输出通道关闭! 11:01:29.827 [pool-1-thread-7] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63958的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.828 [pool-1-thread-7] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63958】的输出通道关闭! 11:01:29.829 [pool-1-thread-3] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63966的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.831 [pool-1-thread-3] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63966】的输出通道关闭! 11:01:29.823 [pool-1-thread-4] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63959的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.832 [pool-1-thread-4] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63959】的输出通道关闭! 11:01:29.832 [pool-1-thread-8] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63963的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.833 [pool-1-thread-8] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63963】的输出通道关闭! 11:01:29.833 [pool-1-thread-5] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63960的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.835 [pool-1-thread-5] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63960】的输出通道关闭! 11:01:29.825 [pool-1-thread-2] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63965】的输出通道关闭! 11:01:29.837 [pool-1-thread-1] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63964的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.838 [pool-1-thread-1] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63964】的输出通道关闭! 11:01:29.845 [pool-1-thread-9] ERROR com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 端口为63967的客户端报错 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.doRequest(RequestRemoteServerWithTcp.java:70) at com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp.run(RequestRemoteServerWithTcp.java:49) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 11:01:29.847 [pool-1-thread-9] INFO com.vincent.fat.core.nio.client.RequestRemoteServerWithTcp - 【63967】的输出通道关闭! Disconnected from the target VM, address: '127.0.0.1:63949', transport: 'socket' Process finished with exit code 0
为了保持长连接,在客户端发送完一次数据以后socketChannel没有关闭,服务端接收完成一次数据以后socketChannel也没有关闭,这样再下次发送数据的时候可以直接发送。但是当客户端线程被中断以后,客户端关闭了sockeChannel,此时服务端没有相应的API判断客户端是否关闭了socket,导致selector在监听事件的时候会无限坚挺到readable()事件,导致CPU等资源大量浪费,系统崩溃。可以通过如下代码来判断客户端socket是否关闭:
int readBytes = 0; while ((readBytes=socketChannel.read(byteBuffer))>0) { //处理数据 content.append(new String(byteBuffer.array(), StandardCharsets.UTF_8)); byteBuffer.clear(); } //判断客户端断开连接 //当客户端没有断开连接的时候readBytes=0这样保持长连接 //当客户端断开连接的时候readBytes=-1 if (readBytes<0) { logger.debug("远程客户端【{}】断开连接.....",socketChannel.getRemoteAddress()); socketChannel.close(); selectionKey.cancel(); } //处理数据 else { logger.info("第【{}】次处理数据,收到的内容是: {}",handleDataNo.incrementAndGet(), content.toString()); }
socketChannel.read()此函数返回客户端读取到的字节数,如果大于0 代表有数据读取到,如果=-1代表客户端关闭了连接,此时可以调用 selectionKey.cancel();取消此channel对相应selector的注册。从而避免无限循环的BUG。