使用ServerSocketChannel的非阻塞accept()
import java.net.InetSocketAddress;/*来自 时 代 J a v a - nowjava.com*/ import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; public class Main { public static void main(String[] argv) throws Exception { int port = 1234; // default ByteBuffer buffer = ByteBuffer.wrap("this is a test".getBytes()); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.socket().bind(new InetSocketAddress(port)); ssc.configureBlocking(false); while (true) { System.out.println("Waiting for connections"); SocketChannel sc = ssc.accept(); if (sc == null) { Thread.sleep(2000); } else { System.out.println(