From bfc2652fa09f2ed3ef665ef61ca3d99bfbd98c9e Mon Sep 17 00:00:00 2001 From: bingor Date: Sat, 6 May 2023 14:33:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=BC=96=E7=A8=8B=E5=9F=BA?= =?UTF-8?q?=E7=A1=80-NIO-=E5=AD=A6=E4=B9=A0-=E8=B0=83=E6=95=B4=E4=BA=86NIO?= =?UTF-8?q?=EF=BC=88=E5=8D=95=E7=BA=BF=E7=A8=8B=EF=BC=89=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/msb/io/nio/Server.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/msb/io/nio/Server.java b/src/main/java/com/msb/io/nio/Server.java index 30be4ea..6f849cd 100644 --- a/src/main/java/com/msb/io/nio/Server.java +++ b/src/main/java/com/msb/io/nio/Server.java @@ -7,6 +7,7 @@ package com.msb.io.nio;/** import java.io.IOException; import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Iterator; @@ -47,9 +48,14 @@ public class Server { private static void handle(SelectionKey key) throws IOException { if(key.isAcceptable()) { - ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); + /*ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); ssc.configureBlocking(false); - ssc.register(key.selector(), SelectionKey.OP_READ); + ssc.register(key.selector(), SelectionKey.OP_READ);*/ + ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); + SocketChannel sc = ssc.accept(); + sc.configureBlocking(false); + sc.register(key.selector(), SelectionKey.OP_READ); + } else if(key.isReadable()) { SocketChannel sc = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(512);