From 8c40948608dbc84e22a20c4ac7404d3c46b068ca Mon Sep 17 00:00:00 2001 From: yiming Date: Thu, 14 Apr 2022 09:57:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E8=A7=89=E6=8E=A7=E5=88=B6=E5=99=A8?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=85=89=E6=BA=90tcp=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/lib/hik/HikLoginModuleImpl.java | 2 +- .../hikLightSource/HikControlSocket.java | 34 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/web/src/main/java/com/zhehekeji/web/lib/hik/HikLoginModuleImpl.java b/web/src/main/java/com/zhehekeji/web/lib/hik/HikLoginModuleImpl.java index 97656d2..edc3bdb 100644 --- a/web/src/main/java/com/zhehekeji/web/lib/hik/HikLoginModuleImpl.java +++ b/web/src/main/java/com/zhehekeji/web/lib/hik/HikLoginModuleImpl.java @@ -16,7 +16,7 @@ public class HikLoginModuleImpl implements CameraControlLoginModule { public static HCNetSDK hcNetsdk = HCNetSDK.INSTANCE; - private static int MAX_RECONNET_TIME = 100000; + private static int MAX_RECONNET_TIME = 100; static HikExceptionCallBack hikExceptionCallBack = new HikExceptionCallBack(); diff --git a/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java b/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java index 1b74241..a31060e 100644 --- a/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java +++ b/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import java.io.*; import java.net.InetSocketAddress; import java.net.Socket; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; /** @@ -18,7 +19,7 @@ public class HikControlSocket { * <${端口号},${开关状态(0:关 1:开)},${get\post}> */ public static void main(String[] args) { - String code = openLight("192.168.8.236", 2002,2,0); + String code = openLight("172.16.0.82", 9000,2,0); System.out.println(code); } @@ -29,13 +30,13 @@ public class HikControlSocket { InputStream is = null; try { socket.connect(new InetSocketAddress(ip,port),3000); + socket.setSoTimeout(10000); os = socket.getOutputStream(); + Thread.sleep(100); controlCmd(os,bool,index); is = socket.getInputStream(); - code = read(is); - if(code!= null){ - code = code.replace("\\n",""); - } + String s = read(socket.getInputStream()); + } catch (IOException e) { log.error("sick time out,ip:{},info:{}",ip,e); }finally { @@ -84,8 +85,27 @@ public class HikControlSocket { } private static String read(InputStream inStream) throws IOException { - BufferedReader bd = new BufferedReader(new InputStreamReader(inStream)); - return bd.readLine(); + StringBuffer sb = new StringBuffer(); + int start = -1; + int end = -1; + do{ + int count = inStream.available(); + if(count > 0){ + byte [] buffer = new byte[count]; + inStream.read(buffer); + String s = new String(buffer, Charset.forName("utf-8")); + sb.append(s); + System.out.println(s); + if(start < 0){ + start = sb.indexOf("<"); + } + if(end <= start){ + end = sb.indexOf(">"); + } + } + + }while (start <0 || end <= start); + return sb.substring(start,end); } }