NIO TCP 通信通过 ServerSocketChannel ,SocketChannel,Selector 来完成
1.服务器端
package com.nio.tcp;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class TCPNIOServer {
public static void main(String[] args) throws IOException {
System.out.println("服务端已经启动,在 8080 端口监听 ......");
ServerSocketChannel channel = ServerSocketChannel.open();// 打开服务通道
channel.configureBlocking(false);// 设置为非阻塞
channel.socket().bind(new InetSocketAddress(8080));// 绑定连接 和 监听端口
startListen(channel);
channel.close();
}
private static void startListen(Server