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