package com.xtkj.test2;
import com.xtkj.vo.Employee;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
public class NioServer1 {
public static void main(String[] args) throws Exception {
new NioServer1().init();
}
/**
* 初始化
* @throws Exception
*/
public void init() throws Exception {
/**
* 获取通道
*/
ServerSocketChannel ssChannel = ServerSocketChannel.open();
/**
* 绑定端口
*/
ssChannel.bind(new InetSocketAddress(9999));
/**
* 设置非阻塞
*/
ssChannel.configureBlocking(false);
/**
* 启动
*/
start(ssChannel);
}
public void start(ServerSocketChannel ssChannel) throws Exception {
/**
* 获取事件管理
*/
Selector selector = Selector.open();
/**
* 注册监听
*/
ssChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
/**
* 获取所有注册的事件
*/
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> ite = keys.iterator();
/**
* 轮询监听
*/
int select = selector.select();
while (ite.hasNext()) {
/**
* 获取当前监听事件对象
*/
SelectionKey next = ite.next();
/**
* 防止事件重复,所有执行完之后删除当前事件
*/
ite.remove();
/**
* 判断是否连接
*/
if (next.isAcceptable()) {
/**
* 如果有里面进来立马获取通道
*/
SocketChannel accept = ssChannel.accept();
/**
* 设置SocketChannel 为非阻塞
*/
accept.configureBlocking(false);
/**
* 在将 读的事件放到事件set中。
*/
accept.register(selector, SelectionKey.OP_READ);
}
/**
* 如果当前时间对象是读
*/
if (next.isReadable()) {
/**
* 获取一个通道
*/
SocketChannel channel = (SocketChannel) next.channel();
/**
* 执行读的方法
*/
read(channel);
/**
* 在设置一个写的事件到set里面
*/
next.interestOps(SelectionKey.OP_WRITE);
}
/**
* 如果是写入事件对象
*/
if (next.isWritable()) {
/**
* 获取通道
*/
SocketChannel channel = (SocketChannel) next.channel();
/**
*
*/
writeFunction(channel);
/**
*
*/
next.interestOps(SelectionKey.OP_READ);
}
}
}
}
/**
* 写入对象
* @param channel
* @throws Exception
*/
private void writeFunction(SocketChannel channel) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream obj = new ObjectOutputStream(bout);
Code code = new Code();
code.setCode(1);
code.setEmployee(new Employee(1, "admin", "男", "湖北咸宁"));
obj.writeObject(code);
obj.flush();
byte[] bytes = bout.toByteArray();
ByteBuffer writebuff = ByteBuffer.wrap(bytes);
channel.write(writebuff);
}
private void read(SocketChannel socketChannel) throws Exception {
/**
* 创建缓存对象
*/
ByteBuffer readBuff = ByteBuffer.allocate(1024);
/**
* 因为可能会重复读所以读指点 可以Clear 当前前面创建了一个新的 所以。。。。
*/
readBuff.clear();
/**
* 将通道中的数据读到 缓存中
*/
socketChannel.read(readBuff);
/**
* 设置读模式
*/
readBuff.flip();
/**
* 调试
*/
System.out.println("readBuff.position() = " + readBuff.position());
System.out.println("readBuff.limit() = " + readBuff.limit());
System.out.println("readBuff.capacity() = " + readBuff.capacity());
String val = new String(readBuff.array(), 0, readBuff.limit());
System.out.println("val = " + val);
switch (val) {
case "1":
System.out.println("1");
break;
case "2":
System.out.println("2");
break;
case "3":
System.out.println("3");
break;
case "4":
System.out.println("4");
break;
}
}
}