下面是时间服务器的代码
NIOTimeServer:
public class NIOTimeServer {
public static void main(String[] args){
//绑定一个用来监听的端口
int port = 8080;
if(args!=null && args.length > 0 ){
try{
port = Integer.valueOf(args[0]);
}catch(NumberFormatException e){
port = 8080;
}
}
//建立一个线程
NIOMultiplexerTimeServer nioMultiplexerTimeServer = new NIOMultiplexerTimeServer(port);
new Thread(nioMultiplexerTimeServer).start();
}
}
NIOMultiplexerTimeServer:
public class NIOMultiplexerTimeServer implements Runnable {
private Selector selector;
private ServerSocketChannel serverSocketChannel;
private volatile boolean stop;
public NIOMultiplexerTimeServer(int port) {
// TODO Auto-generated constructor stub
try {
//打开一个多路复用器
this.selector = Selector.open();
//打开一个通道
this.serverSocketChannel = ServerSocketChannel.open();
//把通道设置成为非阻塞IO
this.serverSocketChannel.configureBlocking(false);
//给通道绑定端口
this.serverSocketChannel.bind(new InetSocketAddress(port), 1024);
//把通道的向对应的多路复用器注册,并且注册对应的事件
this.serverSocketChannel.register(this.selector, SelectionKey.OP_ACCEPT);
System.out.println("The time server is start in port: " + port);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
System.exit(1);
}
}
public void stop() {
this.stop = true;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (!stop) {//不断轮询多路复用器
try {
this.selector.select(1000);
//获取事件
Set<SelectionKey> selectionKeys = selector.selectedKeys();
//迭代事件
Iterator<SelectionKey> it = selectionKeys.iterator();
SelectionKey key = null;
while (it.hasNext()) {
key = it.next();
it.remove();
try {
//处理事件
handleInput(key);
} catch (Exception e) {
// TODO: handle exception
if (key != null) {
key.cancel();
if (key.channel() != null)
key.channel().close();
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
private void handleInput(SelectionKey key) throws IOException {
if (key.isValid()) {
//判断事件类型
if (key.isAcceptable()) {
//获取事件对应的ServerSocketChannel
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
//利用ServerSocketChannel创建SocketChannel
SocketChannel sc = ssc.accept();
//设置成为非阻塞模式
sc.configureBlocking(false);
//将SocketChannel注册进多路复用器
sc.register(this.selector, SelectionKey.OP_READ);
}
if (key.isReadable()) {
//如果是可读事件
SocketChannel socketChannel = (SocketChannel) key.channel();
//分配一个ByteBuffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//把读取内容写入ByteBuffer
int readBytes = socketChannel.read(byteBuffer);
if (readBytes > 0) {
//读写切换
byteBuffer.flip();
//建立读取缓存区
byte[] bytes = new byte[byteBuffer.remaining()];
//读取信息
byteBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("The Time server receive order: " + body);
String currentTime = "QUERY TIME ORDER".equals(body)
? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";// 判断输入是否合法
//写入响应信息
doWrite(socketChannel, currentTime);
} else if (readBytes < 0) {
key.cancel();
socketChannel.close();
} else {
;
}
}
}
}
private void doWrite(SocketChannel socketChannel, String response) throws IOException {
if (response != null && response.trim().length() > 0) {
byte[] bytes = response.getBytes();
ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
byteBuffer.put(bytes);
byteBuffer.flip();
socketChannel.write(byteBuffer);
}
}
}
NIOTimeClient:
public class NIOTimeClient {
public static void main(String[] args){
int port = 8080;
if(args!=null && args.length > 0 ){
try{
port = Integer.valueOf(args[0]);
}catch(NumberFormatException e){
port = 8080;
}
}
new Thread(new NIOTimeClientHandler("127.0.0.1", port), "TimeClient-001")
.start();
}
}
NIOTimeClientHandler:
public class NIOTimeClientHandler implements Runnable {
private String host;
private int port;
private Selector selector;
private SocketChannel socketChannel;
private volatile boolean stop;
public NIOTimeClientHandler(String host, int port) {
// TODO Auto-generated constructor stub
this.host = host==null? "127.0.0.1":host;
this.port = port;
try{
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
System.exit(1);
}
}
@Override
public void run() {
try {
doConnect();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
while (!stop) {
try {
selector.select(1000);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
SelectionKey key = null;
while (it.hasNext()) {
key = it.next();
it.remove();
try {
handleInput(key);
} catch (Exception e) {
if (key != null) {
key.cancel();
if (key.channel() != null)
key.channel().close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
// 多路复用器关闭后,所有注册在上面的Channel和Pipe等资源都会被自动去注册并关闭,所以不需要重复释放资源
if (selector != null)
try {
selector.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleInput(SelectionKey key) throws IOException {
if (key.isValid()) {
// 判断是否连接成功
SocketChannel sc = (SocketChannel) key.channel();
if (key.isConnectable()) {
if (sc.finishConnect()) {
sc.register(selector, SelectionKey.OP_READ);
doWrite(sc);
} else
System.exit(1);// 连接失败,进程退出
}
if (key.isReadable()) {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = sc.read(readBuffer);
if (readBytes > 0) {
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("Now is : " + body);
this.stop = true;
} else if (readBytes < 0) {
// 对端链路关闭
key.cancel();
sc.close();
} else
; // 读到0字节,忽略
}
}
}
private void doConnect() throws IOException {
// 如果直接连接成功,则注册到多路复用器上,发送请求消息,读应答
if (socketChannel.connect(new InetSocketAddress(host, port))) {
socketChannel.register(selector, SelectionKey.OP_READ);
doWrite(socketChannel);
} else
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
private void doWrite(SocketChannel sc) throws IOException {
byte[] req = "QUERY TIME ORDER".getBytes();
ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);
writeBuffer.put(req);
writeBuffer.flip();
sc.write(writeBuffer);
if (!writeBuffer.hasRemaining())
System.out.println("Send order to server succeed.");
}
}