package NotBlockingNetNIO;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class Client {
public static void main(String[] args) {
startClient();
}
public static void startClient() {
try {
SocketChannel sChannel= SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898));
//切换到非阻塞模式
sChannel.configureBlocking(false);
ByteBuffer buf=ByteBuffer.allocate(1024);
buf.put("我爱你".getBytes());
buf.flip();
sChannel.write(buf);
buf.clear();
//关闭
sChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package NotBlockingNetNIO;
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 Server {
public static void main(String[] args) {
startServer();
}
public static void startServer() {
try {
ServerSocketChannel ssChannel=ServerSocketChannel.open();
//切换到非阻塞模式
ssChannel.configureBlocking(false);
ssChannel.bind(new InetSocketAddress(9898));
//获取选择器
Selector selector=Selector.open();
//将通道注册到选择器上,并且指定"监听接收事件"
ssChannel.register(selector,SelectionKey.OP_ACCEPT);
//轮询式的获取选择器上已经“准备就绪”的事件
while(selector.select()>0) {
Iterator<SelectionKey> it= selector.selectedKeys().iterator();
while(it.hasNext()) {
//获取准备就绪的事件
SelectionKey sk=it.next();
//判断具体是什么事件的准备就绪
if(sk.isAcceptable()) {
SocketChannel sChannel=ssChannel.accept();
//切换到非阻塞式模式
sChannel.configureBlocking(false);
//将通道注册到选择器上
sChannel.register(selector, SelectionKey.OP_READ);
}else if(sk.isReadable()) {
SocketChannel sChannel=(SocketChannel) sk.channel();
//读取数据
ByteBuffer buf=ByteBuffer.allocate(1024);
int len=0;
while((len=sChannel.read(buf))>0) {
buf.flip();
System.out.println(new String(buf.array(),0,len));
buf.clear();
}
}
it.remove();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}