From cc6023291bc94a229b53d7c2d83bf2b39bbfa9a0 Mon Sep 17 00:00:00 2001
From: hxb <hxb@hdlchina.com.cn>
Date: 星期日, 27 三月 2022 21:27:29 +0800
Subject: [PATCH] 华尔思线上版本库

---
 HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java |   27 ++++++++++++++++-----------
 1 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
index 3f3c359..5ff1097 100644
--- a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
+++ b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
@@ -3,6 +3,7 @@
 
 
 import com.hdl.sdk.common.utils.ByteUtils;
+import com.hdl.sdk.common.utils.LogUtils;
 import com.hdl.sdk.common.utils.ThreadToolUtils;
 import com.hdl.sdk.socket.SocketBoot;
 import com.hdl.sdk.socket.SocketOptions;
@@ -15,6 +16,8 @@
 import java.io.OutputStream;
 import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.StandardSocketOptions;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -67,12 +70,14 @@
 
     @Override
     public void connect() throws Exception {
+
         mSocket = getSocket();
-        SocketOptions options = getOptions();
-        mSocket.connect(new InetSocketAddress(ip, port));
+//        SocketOptions options = getOptions();
+        mSocket.connect(new InetSocketAddress(ip, port), 3 * 1000);
         mSocket.setTcpNoDelay(true);
         mSocket.setReuseAddress(true);
-        mSocket.setKeepAlive(true);
+//        mSocket.setKeepAlive(true);
+        mSocket.setSoTimeout(25 * 1000);
 
         tcpClientList.add(this);
     }
@@ -103,14 +108,18 @@
     public void onHandleResponse() throws Exception {
         final InputStream stream = getInputStream();
         if (stream != null && getOptions() != null) {
-            int len=0;
-            while ( (len=getInputStream().read(readBuffer)) != -1) {
+            while ( true) {
+                int len=getInputStream().read(readBuffer);
+                if(len<=0){
+                    throw  new Exception("鎺ユ敹寮傚父锛屾帴鏀舵暟鎹暱搴en="+len);
+                }
+
                 IHandleMessage handleMessage = getOptions().getHandleMessage();
                 if (handleMessage != null) {
                     byte []bytes = new byte[len];
                     System.arraycopy(readBuffer,0,bytes,0,len);
                     //瀹屾暣鐨勬暟鎹墠鍥炶皟
-                    handleMessage.read(bytes);
+                    handleMessage.read(bytes,ip);
                 }
             }
         }
@@ -120,14 +129,10 @@
     public void sendMsg(byte[] msg) throws Exception {
         final OutputStream outputStream = getOutStream();
         if (outputStream != null && getOptions() != null) {
-            try {
                 IHandleMessage handleMessage = getOptions().getHandleMessage();
                 handleMessage.write(handleMessage.write(msg));
-                getOutStream().write(msg);
-
-            } finally {
+                outputStream.write(msg);
                 outputStream.flush();
-            }
         }
     }
 

--
Gitblit v1.8.0