服务端

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();
}
}

客户端

public class Client {
private static final String IP = "127.0.0.1";
private static final int PORT = 8888;
private SocketChannel channel;
private Selector selector;
private String user;

//构造函数
public Client() throws IOException {
//打开服务端通道
channel = SocketChannel.open(new InetSocketAddress(IP, PORT));
//设置非阻塞
channel.configureBlocking(false);
//打开选择器
selector = Selector.open();
//把通道注册到选择器,关注的事件是读
channel.register(selector, SelectionKey.OP_READ);
user = channel.getLocalAddress().toString();
System.out.println(user + "已连接...");
}

//向服务器发送消息
private void sendServer() throws IOException {
//拿到文件
File file = new File("d:\\test\\write.txt");
FileInputStream inputStream = new FileInputStream(file);
//创建管道,把文件放入通道
FileChannel fileChannel = inputStream.getChannel();
long transferCount = fileChannel.transferTo(0, fileChannel.size(), channel);
System.out.println("发送字节数" + transferCount);
//fileChannel.close();
}

public static void main(String[] args) throws Exception {
Client client = new Client();
client.sendServer();
}
}