public class Server {
//backlog为accept队列大小,默认值为50
private static final int BACKLOG = 1024;
private static final String IP = "127.0.0.1";
private static final int PORT = 8888;
private ServerSocketChannel serverChannel;
private Selector selector;
//构造函数
public Server() {
try {
//打开服务端通道
serverChannel = ServerSocketChannel.open();
//设置非阻塞
serverChannel.configureBlocking(false);
//绑定端口,在服务端监听
serverChannel.bind(new InetSocketAddress(IP, PORT), BACKLOG);
//打开选择器
selector = Selector.open();
//把服务端通道注册到选择器,关注的事件是接收
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
e.printStackTrace();
}
}
//监听客户端
public void listen() {
try {
//循环等待客户端连接
for (; ; ) {
int readChannel = selector.select();
if (readChannel > 0) {
//关注事件集合
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
//如果是OP_ACCEPT,有新的客户端连接
if (key.isAcceptable()) {
SocketChannel channel = serverChannel.accept();
channel.configureBlocking(false);
//把通道注册到选择器,关注的事件是读
channel.register(selector, SelectionKey.OP_READ);
System.out.println(channel.getRemoteAddress() + "已连接,准备传输...");
}
if (key.isReadable()) {
readClient(key);
}
//移除Selection,因为多线程,要防止重复操作
iterator.remove();
}
} else {
System.out.println("没有可用通道!");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
//读取客户端
private void readClient(SelectionKey key) {
SocketChannel channel = null;
try {
channel = (SocketChannel) key.channel();
//获取buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
//把通道数据放入缓冲区
int count = channel.read(buffer);
if (count > 0) {
//反转读写模式
buffer.flip();
//创建fileChannel,把文件放入通道
FileChannel fileChannel = new FileOutputStream("d:\\test\\write888.txt").getChannel();
//把Buffer内容放入fileChannel
fileChannel.write(buffer);
//fileChannel.transferFrom(channel, 0, 1024);
}
} catch (IOException e) {
try {
System.out.println(channel.getRemoteAddress() + "断线了...");
//取消注册
key.cancel();
//关闭通道
channel.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
Server server = new Server();
server.listen();
}
}