|
@@ -0,0 +1,83 @@
|
|
|
|
|
+package cn.reghao.dfs.store.sync;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.net.InetSocketAddress;
|
|
|
|
|
+import java.nio.ByteBuffer;
|
|
|
|
|
+import java.nio.channels.*;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.util.Iterator;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author reghao
|
|
|
|
|
+ * @date 2022-08-26 15:07:46
|
|
|
|
|
+ */
|
|
|
|
|
+public class FileClient {
|
|
|
|
|
+ private Selector selector;
|
|
|
|
|
+
|
|
|
|
|
+ public FileClient init(String host, int port) throws IOException, InterruptedException {
|
|
|
|
|
+ SocketChannel socketChannel = SocketChannel.open();
|
|
|
|
|
+ socketChannel.configureBlocking(false);
|
|
|
|
|
+ selector = Selector.open();
|
|
|
|
|
+ socketChannel.connect(new InetSocketAddress(host, port));
|
|
|
|
|
+ socketChannel.register(selector, SelectionKey.OP_CONNECT);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void start() throws IOException {
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ selector.select();
|
|
|
|
|
+ Iterator<SelectionKey> it = selector.selectedKeys().iterator();
|
|
|
|
|
+ while (it.hasNext()) {
|
|
|
|
|
+ SelectionKey key = it.next();
|
|
|
|
|
+ it.remove();
|
|
|
|
|
+ if (key.isConnectable()) {
|
|
|
|
|
+ SocketChannel client = (SocketChannel) key.channel();
|
|
|
|
|
+ if (client.isConnectionPending()) {
|
|
|
|
|
+ client.finishConnect();
|
|
|
|
|
+ }
|
|
|
|
|
+ client.configureBlocking(false);
|
|
|
|
|
+ client.write(ByteBuffer.wrap(new String("halo server").getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
+
|
|
|
|
|
+ client.register(selector, SelectionKey.OP_READ);
|
|
|
|
|
+ System.out.println("客户端连接到服务端");
|
|
|
|
|
+ } else if (key.isReadable()) {
|
|
|
|
|
+ System.out.println("客户端从服务端读取数据");
|
|
|
|
|
+
|
|
|
|
|
+ String filePath = "/home/reghao/Downloads/haha.dat";
|
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
|
+ file.createNewFile();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
+ FileChannel fileChannel = fos.getChannel();
|
|
|
|
|
+
|
|
|
|
|
+ SocketChannel channel = (SocketChannel) key.channel();
|
|
|
|
|
+ ByteBuffer byteBuffer = ByteBuffer.allocate(1024*1024*10);
|
|
|
|
|
+ int readBytes = 0;
|
|
|
|
|
+ while (readBytes != -1) {
|
|
|
|
|
+ readBytes = channel.read(byteBuffer);
|
|
|
|
|
+ byteBuffer.flip();
|
|
|
|
|
+
|
|
|
|
|
+ fileChannel.write(byteBuffer);
|
|
|
|
|
+ fos.flush();
|
|
|
|
|
+ byteBuffer.clear();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fileChannel.close();
|
|
|
|
|
+ fos.close();
|
|
|
|
|
+ channel.close();
|
|
|
|
|
+ System.out.println("客户端从服务端读取数据完成");
|
|
|
|
|
+ } else if (key.isWritable()) {
|
|
|
|
|
+ System.out.println("客户端写数据到服务端");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) throws IOException, InterruptedException {
|
|
|
|
|
+ new FileClient().init("localhost", 8020).start();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|