Skip to content

Commit

Permalink
优化tcpclient
Browse files Browse the repository at this point in the history
  • Loading branch information
longqingzhao committed Feb 16, 2019
1 parent 61735ce commit 20ae358
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public void run() {
while (selector.select() > 0) {
//返回的是select的key集合并遍历(如果一个key所指的对象为空会报异常)
for (SelectionKey key : selector.selectedKeys()) {
if (sc == null || !sc.isConnected()) {
return;
}
//如果该SelectionKey对应的Channel中有可读的数据
if (key.isReadable()) {
ByteBuffer buff = ByteBuffer.allocate(1024);
Expand All @@ -148,9 +151,6 @@ public void run() {
if (nioResponse != null) {
nioResponse.receive(content.toString());
}
if (sc == null || !sc.isConnected()) {
return;
}
//为下一次读取作准备
key.interestOps(SelectionKey.OP_READ);
}
Expand Down

0 comments on commit 20ae358

Please sign in to comment.