Explorar o código

feat:完成客户端到服务端之间的读写

yang yi hai 2 meses
pai
achega
f258a42950

+ 8 - 0
chat-gwng/chat-client/pom.xml

@@ -16,4 +16,12 @@
         <maven.compiler.target>17</maven.compiler.target>
     </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>anyi.space</groupId>
+            <artifactId>chat-commom</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
 </project>

+ 33 - 0
chat-gwng/chat-client/src/main/java/space/anyi/chatClient/Client.java

@@ -0,0 +1,33 @@
+package space.anyi.chatClient;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @ProjectName: chat-gwng
+ * @FileName: Client
+ * @Author: 杨逸
+ * @Data:2025/9/22 10:47
+ * @Description:
+ */
+public class Client {
+    public static void main(String[] args) throws IOException, InterruptedException {
+        String host = "localhost";
+        int port = 8000;
+        InetSocketAddress inetSocketAddress = new InetSocketAddress(host, port);
+        //创建连接
+        SocketChannel socketChannel = SocketChannel.open(inetSocketAddress);
+        //写
+        socketChannel.write(ByteBuffer.wrap("Hello World!".getBytes(StandardCharsets.UTF_8)));
+        //读
+        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
+        int len = socketChannel.read(byteBuffer);
+        byte[] array = byteBuffer.array();
+        System.out.println(new String(array, 0, len));
+        //释放资源
+        socketChannel.close();
+    }
+}

+ 8 - 0
chat-gwng/chat-server/pom.xml

@@ -16,4 +16,12 @@
         <maven.compiler.target>17</maven.compiler.target>
     </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>anyi.space</groupId>
+            <artifactId>chat-commom</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
 </project>

+ 39 - 0
chat-gwng/chat-server/src/main/java/space/anyi/chatServer/Server.java

@@ -0,0 +1,39 @@
+package space.anyi.chatServer;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @ProjectName: chat-gwng
+ * @FileName: Server
+ * @Author: 杨逸
+ * @Data:2025/9/22 10:42
+ * @Description:
+ */
+public class Server {
+    public static void main(String[] args) throws IOException, InterruptedException {
+        int port = 8000;
+        InetSocketAddress inetSocketAddress = new InetSocketAddress(port);
+        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
+        //监听
+        serverSocketChannel.bind(inetSocketAddress);
+
+        //创建连接
+        SocketChannel socketChannel = serverSocketChannel.accept();
+
+        //读
+        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
+        int len = socketChannel.read(byteBuffer);
+        System.out.println(new String(byteBuffer.array(),0,len));
+        //写
+        ByteBuffer wrap = ByteBuffer.wrap("server send message".getBytes(StandardCharsets.UTF_8));
+        socketChannel.write(wrap);
+        //释放资源
+        socketChannel.close();
+        serverSocketChannel.close();
+    }
+}