From cd9de930c6f3963a01b3a1679797121f099a8054 Mon Sep 17 00:00:00 2001 From: seungtaekhong Date: Thu, 11 Jan 2024 22:21:36 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20socket=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/ClientInferenceController.java | 8 +--- .../inference/service/SocketClient.java | 42 ++++++++++++------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/main/java/solution/gdsc/PathPal/domain/inference/api/ClientInferenceController.java b/src/main/java/solution/gdsc/PathPal/domain/inference/api/ClientInferenceController.java index 4c36e06..579562d 100644 --- a/src/main/java/solution/gdsc/PathPal/domain/inference/api/ClientInferenceController.java +++ b/src/main/java/solution/gdsc/PathPal/domain/inference/api/ClientInferenceController.java @@ -1,6 +1,5 @@ package solution.gdsc.PathPal.domain.inference.api; -import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import org.springframework.web.socket.BinaryMessage; import org.springframework.web.socket.CloseStatus; @@ -19,7 +18,6 @@ import java.util.concurrent.ConcurrentHashMap; @Component -@Profile("!test") public class ClientInferenceController extends BinaryWebSocketHandler { private final String hostName = "127.0.0.1"; @@ -31,11 +29,7 @@ public class ClientInferenceController extends BinaryWebSocketHandler { public ClientInferenceController(InferenceService inferenceService) { this.inferenceService = inferenceService; - try { - this.socketClient = new SocketClient(hostName, port, 2000); - } catch (IOException e) { - throw new RuntimeException("SocketClient 생성 실패", e); - } + this.socketClient = new SocketClient(hostName, port, 2000); } @Override diff --git a/src/main/java/solution/gdsc/PathPal/domain/inference/service/SocketClient.java b/src/main/java/solution/gdsc/PathPal/domain/inference/service/SocketClient.java index 0e71fb1..8ba31a9 100644 --- a/src/main/java/solution/gdsc/PathPal/domain/inference/service/SocketClient.java +++ b/src/main/java/solution/gdsc/PathPal/domain/inference/service/SocketClient.java @@ -8,6 +8,7 @@ import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketTimeoutException; import java.util.ArrayList; @@ -23,7 +24,7 @@ public class SocketClient { private BufferedInputStream bis; private BufferedOutputStream bos; - public SocketClient(String hostName, int port, int timeout) throws IOException { + public SocketClient(String hostName, int port, int timeout) { this.port = port; this.timeout = timeout; this.hostName = hostName; @@ -32,18 +33,32 @@ public SocketClient(String hostName, int port, int timeout) throws IOException { socketConnect(); } - private void socketConnect() throws IOException { - if (this.socket != null && this.socket.isConnected()) { - this.socket.close(); + private void socketConnect() { + if (this.socket != null) { + if (socket.isConnected() && !socket.isClosed()) { + try { + this.socket.close(); + } catch (IOException e) { + } + } + } + this.socket = new Socket(); + try { + this.socket.setSoTimeout(timeout); + } catch (IOException e) { + } + + try { + this.socket.connect(new InetSocketAddress(hostName, port), timeout); + this.bos = new BufferedOutputStream(socket.getOutputStream()); + this.bis = new BufferedInputStream(socket.getInputStream()); + } catch (IOException e) { + System.err.println("소켓 연결 실패. 프로그램을 종료합니다."); + System.exit(1); } - this.socket = new Socket(hostName, port); - this.socket.setSoTimeout(timeout); - this.bos = new BufferedOutputStream(socket.getOutputStream()); - this.bis = new BufferedInputStream(socket.getInputStream()); } public List inferenceImage(byte[] file) { - try { // 1. Send File Size System.out.println("파일 사이즈 전송:" + file.length); @@ -73,13 +88,8 @@ public List inferenceImage(byte[] file) { } catch (SocketTimeoutException e) { System.err.println("소켓 타임아웃 발생"); System.err.println("소켓 재연결 시도"); - try { - socketConnect(); - return inferenceImage(file); - } catch (IOException ioException) { - System.err.println("소켓 재연결 실패"); - return new ArrayList<>(); - } + socketConnect(); + return inferenceImage(file); } catch (Exception e) { e.printStackTrace(); return new ArrayList<>();