网络编程基础-NIO-学习-调整了NIO(单线程)模式的代码错误

master
bingor 2 years ago
parent 9f9adf8ab2
commit bfc2652fa0

@ -7,6 +7,7 @@ package com.msb.io.nio;/**
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.Iterator; import java.util.Iterator;
@ -47,9 +48,14 @@ public class Server {
private static void handle(SelectionKey key) throws IOException { private static void handle(SelectionKey key) throws IOException {
if(key.isAcceptable()) { if(key.isAcceptable()) {
ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); /*ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
ssc.configureBlocking(false); 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()) { } else if(key.isReadable()) {
SocketChannel sc = (SocketChannel) key.channel(); SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(512); ByteBuffer buffer = ByteBuffer.allocate(512);

Loading…
Cancel
Save