邵洋 1 год назад
Родитель
Сommit
b1dd0a773f

+ 43 - 13
lamp-service/src/main/java/com/zksy/lamp/server/ExecutionServer.java

@@ -4,7 +4,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import java.io.*;
+import java.net.ServerSocket;
 import java.net.Socket;
 
 /**
@@ -16,28 +18,56 @@ import java.net.Socket;
  */
 @Component
 public class ExecutionServer {
-    @Autowired
-    private TcpServer tcpServer;
     private BufferedReader reader;
     private BufferedWriter writer;
+    private ServerSocket server;
+    private Socket client;
+    private int serverPort = 15678;
+
     @PostConstruct
-    public void init() {
+    public void init(){
+        Runnable func0 = this::startServer;
+        Thread thread = new Thread(func0);
+        thread.start();
+    }
+
+    @PreDestroy
+    public void closeResource(){
         try {
-            // 等待 TcpServer 初始化完成
-            tcpServer.awaitInitialization();
-            reader = tcpServer.getReader();
-            writer = tcpServer.getWriter();
-            if (reader == null || writer == null) {
-                throw new IOException("Reader or Writer is not available.");
-            }
-            System.out.println("Reader and Writer initialized successfully.");
+            if (reader != null) reader.close();
+            if (writer != null) writer.close();
+            if (client != null) client.close();
+            if (server != null) server.close();
         } catch (IOException e) {
             e.printStackTrace();
-        } catch (InterruptedException e) {
-            throw new RuntimeException(e);
         }
     }
 
+    /**
+     * 启动服务器并等待客户端连接
+     */
+    public void startServer() {
+        try {
+            System.out.println("本机作为服务端");
+            System.out.println("本机端口:" + serverPort);
+            server = new ServerSocket(serverPort);
+
+            System.out.println("等待客户机的连接");
+            client = server.accept();
+            System.out.println("连接成功");
+
+            String clientAddr = client.getRemoteSocketAddress().toString().substring(1);
+            String[] clientAddrs = clientAddr.split(":");
+            System.out.println("客户端IP:" + clientAddrs[0] + " 端口:" + clientAddrs[1]);
+
+            reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));
+            writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "GB2312"));
+            System.out.println("测试完成!");
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
     public String ExecutionData(String data) {
         System.out.println("writer==================="+writer);
         System.out.println("reader==================="+reader);

+ 6 - 3
lamp-service/src/main/java/com/zksy/lamp/server/TcpServer.java

@@ -1,3 +1,4 @@
+/*
 package com.zksy.lamp.server;
 
 import cn.hutool.core.lang.func.VoidFunc0;
@@ -38,9 +39,11 @@ public class TcpServer {
         }
     }
 
-    /**
+    */
+/**
      * 启动服务器并等待客户端连接
-     */
+     *//*
+
     public void startServer() {
         try {
             System.out.println("本机作为服务端");
@@ -79,4 +82,4 @@ public class TcpServer {
             lock.wait(); // 阻塞当前线程,直到有通知
         }
     }
-}
+}*/