/**
*
*/
package Test;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
/**
*
* @author Administrator
* @version
*/
public class NBTest {
/** Creates new NBTest */
public NBTest() {
}
public void startServer() throws Exception {
int channels = 0;
int nKeys = 0;
int currentSelector = 0;
// 使用Selector
Selector selector = Selector.open();
// 建立Channel 并绑定到9000端口
ServerSocketChannel ssc = ServerSocketChannel.open();
InetSocketAddress address = new InetSocketAddress(
InetAddress.getLocalHost(), 9000);
ssc.socket().bind(address);
// 使设定non-blocking的方式。
ssc.configureBlocking(false);
// 向Selector注册Channel及我们有兴趣的事件
SelectionKey s = s
java7 nio 例子
最新推荐文章于 2021-02-26 00:20:15 发布