From a3f9e5ce8dce3d86a60a79b85ce44424a612c6fa Mon Sep 17 00:00:00 2001 From: 562935844@qq.com Date: 星期五, 13 五月 2022 14:06:45 +0800 Subject: [PATCH] 提交jar版本 --- HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java | 69 +++++++++++++++++----------------- 1 files changed, 35 insertions(+), 34 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..b4a7d3c 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 @@ -1,8 +1,6 @@ package com.hdl.sdk.socket.client; - - -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 +13,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; @@ -36,21 +36,21 @@ /** * 浠庤繛鎺ユ睜涓壘鍑哄綋鍓岻P鍙婄鍙g殑杩炴帴瀹㈡埛绔� + * * @param ipAdderss 杩炴帴IP鍦板潃 - * @param port 杩炴帴绔彛 + * @param port 杩炴帴绔彛 * @return */ - public static TcpClient getTcpClientByIP(String ipAdderss,int port) { - for(TcpClient tcpClient:tcpClientList){ - if(tcpClient.ip.equals(ipAdderss)&&tcpClient.port==port) - { + public static TcpClient getTcpClientByIP(String ipAdderss, int port) { + for (TcpClient tcpClient : tcpClientList) { + if (tcpClient.ip.equals(ipAdderss) && tcpClient.port == port) { return tcpClient; } } - return null; + return null; } - private byte[] readBuffer = new byte[4*1024]; + private byte[] readBuffer = new byte[4 * 1024]; private TcpClient(String ip, int port, SocketOptions socketOptions) { this.socketOptions = socketOptions; @@ -67,17 +67,17 @@ @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); } - - @Override @@ -103,14 +103,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); + byte[] bytes = new byte[len]; + System.arraycopy(readBuffer, 0, bytes, 0, len); //瀹屾暣鐨勬暟鎹墠鍥炶皟 - handleMessage.read(bytes); + handleMessage.read(bytes, ip); } } } @@ -120,14 +124,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.flush(); - } + IHandleMessage handleMessage = getOptions().getHandleMessage(); + handleMessage.write(handleMessage.write(msg)); + outputStream.write(msg); + outputStream.flush(); } } @@ -136,6 +136,7 @@ * 澶勭悊杩炴帴鐘舵�� */ public void onConnectStatus(int status) { + ThreadToolUtils.getInstance().runOnUiThread(new Runnable() { @Override public void run() { @@ -143,16 +144,13 @@ if (list != null && !list.isEmpty()) { for (ConnectStatusListener listener : list) { switch (status) { - case ConnectStatus - .CONNECTING: + case 0: listener.onConnecting(); break; - case ConnectStatus - .CONNECTED: + case 1: listener.onConnected(); break; - case ConnectStatus - .DISCONNECT: + case 2: listener.onConnectFailed(); break; } @@ -160,6 +158,9 @@ } } }); + + LogUtils.i("TcpClient onConnectStatus status:" + status); + } -- Gitblit v1.8.0