gpt4 book ai didi

java - 读取套接字响应时,线程在几次迭代后 hibernate

转载 作者:行者123 更新时间:2023-11-30 11:34:39 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我在其中通过套接字进行写入和读取。然而,它只执行了 11 次任务,然后就 hibernate 了。

PollThread.java

public class PollThread {

static String result;
private static Timer myTimer;
static String ip = "192.168.1.19";

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("PollThread");
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
ClientThread cThread = new ClientThread(ip);
String status = cThread.readStatus();
System.out.println("Staus :: "+status);
}
}, 0, 2000);

}
}

ClientThread.java

public class ClientThread {

String byte_to_hex, swapped_result, result,ipAddr;
public Socket s;
public InputStream i;
public OutputStream o;
int status;

public ClientThread(String ip) {
// TODO Auto-generated constructor stub
this.ipAddr=ip;

}

public int readStatus() {
try {
s = new Socket(ipAddr, 502);
i = s.getInputStream();
o = s.getOutputStream();

byte[] data1 = new byte[1024], packet1 = { (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x06, (byte) 0x01, (byte) 0x01, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x19 };

o.write(packet1);
i.read(data1, 0, 1024);//Comment this
byte_to_hex = bytesToHex(data1).substring(18, 26);

char[] arr = byte_to_hex.toCharArray();
for (int i = 0; i < arr.length - 1; i += 2) {
char temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}

swapped_result = new String(arr);

result = hexStringToNBitBinary(swapped_result, 32);

int counter = 0;
for (int i = 0; i < result.length(); i++) {
if (result.charAt(i) == '1') {
counter++;
}
}
status = counter;

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
}

结果

Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1

如果我注释掉 i.read(data1, 0, 1024); 那么它工作正常,但我需要这一行来获得结果。

这可能是什么问题?为什么它只运行了 11 次?

UPDATE-1

Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...

更新 2

我也尝试在 PollThread.java 文件中添加这一行 cThread.start(); 但结果是一样的。

最佳答案

正如其他评论者所提到的,您的计时器实际上正在创建多个客户端连接,每两秒一个,而不是定期测试的单个连接。您的“服务器模拟器”的最大连接数很可能是 10 或 11。尝试将您的客户端连接移到 TimerTask 之外,这样它就不会每次都创建:

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("PollThread");
myTimer = new Timer();
final ClientThread cThread = new ClientThread(ip);
myTimer.schedule(new TimerTask() {
@Override
public void run() {
String status = cThread.readStatus();
System.out.println("Staus :: "+status);
}
}, 0, 2000);
}

抱歉,刚刚注意到您在 readStatus 方法中创建了一个新的 Socket,这本质上是相同的多连接问题。尝试将这些东西移动到构造函数中,例如:

public ClientThread(String ip) {
// TODO Auto-generated constructor stub
this.ipAddr=ip;

try {
s = new Socket(ipAddr, 502);
i = s.getInputStream();
o = s.getOutputStream();
// ...
}

public int readStatus() {
try {
//s = new Socket(ipAddr, 502);
//i = s.getInputStream();
//o = s.getOutputStream();

关于java - 读取套接字响应时,线程在几次迭代后 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15544966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com