/*** TcpAsyncServer.java*/
importjava.nio.ByteBuffer;import java.nio.channels.*;import java.net.*;importjava.util.Iterator;public classTcpAsyncServer {/*监听端口*/
int port = 6000;/*缓冲区大小*/ByteBuffer buffer= ByteBuffer.allocate(1024);/*其它相关定义*/Selector selector;
ServerSocketChannel channel;
ServerSocket socket;/*启动*/
public void Start() throwsException {/*初始化一个Selector*/selector=Selector.open();/*打开通道*/channel=ServerSocketChannel.open();/*非阻塞模式*/channel.configureBlocking(false);/*本机IP*/
//InetAddress ip = InetAddress.getByName("127.0.0.1");
InetAddress ip =InetAddress.getLocalHost();
System.out.println(ip.toString());/*绑定IP和端口*/InetSocketAddress address= newInetSocketAddress(ip,port);
socket=channel.socket();
socket.bind(address);/*启动监听*/System.out.println("TCP服务器开始监听...");
Listen();
}/*停止*/
p