Commit 16a45a69 authored by wanghao's avatar wanghao

1 每次读取后 关闭 socket连接

parent 223e5917
...@@ -3,12 +3,14 @@ package com.zehong; ...@@ -3,12 +3,14 @@ package com.zehong;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* 启动程序 * 启动程序
* *
* @author zehong * @author zehong
*/ */
@EnableScheduling
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class ZhMesAgecalApplication public class ZhMesAgecalApplication
{ {
......
...@@ -53,7 +53,6 @@ public class EquipmentDataCollection { ...@@ -53,7 +53,6 @@ public class EquipmentDataCollection {
log.error("设备列表查询结果为空"); log.error("设备列表查询结果为空");
throw new Exception("无设备信息!"); throw new Exception("无设备信息!");
} }
if(equipmentInfos.size() > 0){
String sendMsg = "00010000000601040000000A"; String sendMsg = "00010000000601040000000A";
String receiveMsg; String receiveMsg;
...@@ -76,7 +75,7 @@ public class EquipmentDataCollection { ...@@ -76,7 +75,7 @@ public class EquipmentDataCollection {
alarmData.setfEquipmentCode(info.getfEquipmentCode()); alarmData.setfEquipmentCode(info.getfEquipmentCode());
alarmData.setfAlarmData("老化柜IP或端口号为空"); alarmData.setfAlarmData("老化柜IP或端口号为空");
tEquipmentAlarmDataService.insertTEquipmentAlarmData(alarmData); tEquipmentAlarmDataService.insertTEquipmentAlarmData(alarmData);
} else if (receiveMsg.length() == 58){ } else {
// 解析服务器返回信息 // 解析服务器返回信息
// 示例:00 01 00 00 00 17 01 04 14 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // 示例:00 01 00 00 00 17 01 04 14 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
String flag = receiveMsg.substring(18, 58); String flag = receiveMsg.substring(18, 58);
...@@ -110,7 +109,6 @@ public class EquipmentDataCollection { ...@@ -110,7 +109,6 @@ public class EquipmentDataCollection {
} }
} }
} }
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -12,7 +12,7 @@ import java.net.Socket; ...@@ -12,7 +12,7 @@ import java.net.Socket;
* *
* @author zehong * @author zehong
*/ */
public class ReceiverThread extends Thread implements Runnable { public class ReceiverThread extends Thread {
private static final Logger log = LoggerFactory.getLogger(ReceiverThread.class); private static final Logger log = LoggerFactory.getLogger(ReceiverThread.class);
public static String hexResponse; public static String hexResponse;
private final Socket socket; private final Socket socket;
...@@ -34,7 +34,11 @@ public class ReceiverThread extends Thread implements Runnable { ...@@ -34,7 +34,11 @@ public class ReceiverThread extends Thread implements Runnable {
// 将接收到的字节转换为十六进制字符串显示 // 将接收到的字节转换为十六进制字符串显示
hexResponse = HexUtils.bytesToHex(responseBytes); hexResponse = HexUtils.bytesToHex(responseBytes);
log.info("收到服务器响应: " + hexResponse); log.info("收到服务器响应: " + hexResponse);
if(hexResponse.length() == 58) {
socket.close();
} }
}
log.info("解析完了啊啊啊啊啊: " + hexResponse);
} catch (IOException e) { } catch (IOException e) {
log.error("接收数据失败: " + e.getMessage()); log.error("接收数据失败: " + e.getMessage());
} }
......
...@@ -34,13 +34,15 @@ public class TCPClient { ...@@ -34,13 +34,15 @@ public class TCPClient {
log.info("已发送数据: " + sendMsg); log.info("已发送数据: " + sendMsg);
receiverThread = new ReceiverThread(socket); receiverThread = new ReceiverThread(socket);
hexResponse = receiverThread.hexResponse;
receiverThread.start(); receiverThread.start();
// receiverThread.join(); receiverThread.join();
hexResponse = ReceiverThread.hexResponse;
} catch (IOException e) { } catch (IOException e) {
System.err.println("通信异常: " + e.getMessage()); System.err.println("通信异常: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} }
return hexResponse; return hexResponse;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment