package nio;
import java.io.IOException;
import java.net.InetAddress;
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;
/**
* 一个简单的NIO 服务的例子
* @author Kaka
*
*/
public class NIOServer {
//ServerSocketChannel 服务channel
ServerSocketChannel ssc=null;
//分发器
Selector selector=null;
//选择键
SelectionKey sk=null;
/**
* 构造函数
* @throws IOException
*/
NIOServer() throws IOException{
ssc=ServerSocketChannel.open();
ssc.configureBlocking(false);
InetAddress ia=InetAddress.getLocalHost();
InetSocketAddress isa=new InetSocketAddress(ia,9000);
ssc.socket().bind(isa);
selector=Selector.open();
sk=ssc.register(selector, SelectionKey.OP_ACCEPT);
}
/**
* 服务器运行
* @throws IOException
*/
void run() throws IOException{
while (true) {
//取得事件数目
int n=selector.select();
if(n==0)
continue;
Iterator<SelectionKey> it=selector.selectedKeys().iterator();
while(it.hasNext())
{
System.out.println("have...");
SelectionKey key = (SelectionKey)it.next();
it.remove();
//轮训连接
if(key.isAcceptable()){
System.out.println("Acceptable");
ServerSocketChannel server =
(ServerSocketChannel) key.channel();
SocketChannel channel = server.accept();//获取SocketChannel来通信
;
channel.configureBlocking(false);
//这个地方可以对channel进行处理,这里只是简单的往客户端写一个字符串
channel.register(selector, SelectionKey.OP_READ);
channel.write(ByteBuffer.wrap("hello".getBytes()));
channel.close();
} //其它的轮训
else if (key.isReadable()) { // 读信息
System.out.println("readable");
}
}
}
}
public static void main(String[] args) throws IOException {
NIOServer server =new NIOServer();
server.run();
}
}
这时候可以使用telnet ip地址 9000来访问这个服务了
正常情况应该能看到hello,然后是连接丢失的信息
import java.io.IOException;
import java.net.InetAddress;
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;
/**
* 一个简单的NIO 服务的例子
* @author Kaka
*
*/
public class NIOServer {
//ServerSocketChannel 服务channel
ServerSocketChannel ssc=null;
//分发器
Selector selector=null;
//选择键
SelectionKey sk=null;
/**
* 构造函数
* @throws IOException
*/
NIOServer() throws IOException{
ssc=ServerSocketChannel.open();
ssc.configureBlocking(false);
InetAddress ia=InetAddress.getLocalHost();
InetSocketAddress isa=new InetSocketAddress(ia,9000);
ssc.socket().bind(isa);
selector=Selector.open();
sk=ssc.register(selector, SelectionKey.OP_ACCEPT);
}
/**
* 服务器运行
* @throws IOException
*/
void run() throws IOException{
while (true) {
//取得事件数目
int n=selector.select();
if(n==0)
continue;
Iterator<SelectionKey> it=selector.selectedKeys().iterator();
while(it.hasNext())
{
System.out.println("have...");
SelectionKey key = (SelectionKey)it.next();
it.remove();
//轮训连接
if(key.isAcceptable()){
System.out.println("Acceptable");
ServerSocketChannel server =
(ServerSocketChannel) key.channel();
SocketChannel channel = server.accept();//获取SocketChannel来通信
;
channel.configureBlocking(false);
//这个地方可以对channel进行处理,这里只是简单的往客户端写一个字符串
channel.register(selector, SelectionKey.OP_READ);
channel.write(ByteBuffer.wrap("hello".getBytes()));
channel.close();
} //其它的轮训
else if (key.isReadable()) { // 读信息
System.out.println("readable");
}
}
}
}
public static void main(String[] args) throws IOException {
NIOServer server =new NIOServer();
server.run();
}
}
这时候可以使用telnet ip地址 9000来访问这个服务了
正常情况应该能看到hello,然后是连接丢失的信息