Skip to content

Commit

Permalink
fix: fix socket write error
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrylususu committed Aug 25, 2024
1 parent 7c07d30 commit 89af61e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/example/Ocr.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ private OcrResponse sendJsonToOcr(Map<String, String> reqJson) throws IOExceptio
StringWriter sw = new StringWriter();
EscapedWriter ew = new EscapedWriter(sw);
gson.toJson(reqJson, ew);

// 重建 socket,修复长时间无请求时 socket 断开(Software caused connection abort: socket write error )
// https://github.com/hiroi-sora/PaddleOCR-json/issues/106
if (OcrMode.SOCKET_SERVER == mode) {
writer.close();
reader.close();
clientSocket.close();
clientSocket = new Socket(serverAddr, serverPort);
clientSocket.setKeepAlive(true);
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(), StandardCharsets.UTF_8));
}

writer.write(sw.getBuffer().toString());
writer.write("\r\n");
writer.flush();
Expand Down

0 comments on commit 89af61e

Please sign in to comment.