gpt4 book ai didi

java - 服务器 : Socket hangs within unpredictable period time at read stream function

转载 作者:行者123 更新时间:2023-12-02 03:29:15 25 4
gpt4 key购买 nike

我编写了一个 Java 套接字服务器,它将保持连接处于 Activity 状态,直到客户端断开连接。我的客户端代码将继续将消息推送到该服务器应用程序。

但是当我运行这些程序一段时间时,我也发现了一种不寻常的情况,即服务器在不可预测的时间内从客户端读取输入流时会挂起。它总是卡在 inData.read(b) 处,因为当这个问题发生时,我看到它在日志上打印出“正在接收...””;即使我杀死了我的客户端,服务器应用程序仍然卡在那里。

但是,当我在发生此问题后在运行服务器应用程序的控制台上按 Ctrl+C 时,它将继续工作。这实在是太烦人了。

有什么办法可以很好地解决这个不寻常的问题吗?

服务器代码:

    static ServerSocket server;

try {
server = new ServerSocket("1234");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Socket socket = null;
String inIp = null;
BufferedInputStream inData;
BufferedOutputStream outData;

while (true) {
try {
synchronized (server) {
socket = server.accept();
}
inIp = String.valueOf(socket.getInetAddress());
if (Log4j.log.isEnabledFor(Level.INFO)) {
Log4j.log.info("Incoming connection " + inIp);
}
while (true) {
inData = new BufferedInputStream(socket.getInputStream());
outData = new BufferedOutputStream(socket.getOutputStream());
String reply = "Hey";

byte[] b = new byte[10240];
String data = "";
int length;

if (Log4j.log.isEnabledFor(Level.INFO)) {
Log4j.log.info("InetAddr = " + inIp + ", receiving...");
}


// read input stream
length = inData.read(b);
data += new String(b, 0, length);
if (Log4j.log.isEnabledFor(Level.INFO)) {
Log4j.log.info("Data Length: " + length + ", Received data: " + data);
}


// output result
outData.write(reply.getBytes());
outData.flush();
}
} catch (Exception e) {
String tempStr = e.toString();
Log4j.log.error("Service error during executing: " + tempStr);
}
}

客户端代码:

    Socket client = new Socket();
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", "1234");
String data = "Hi";

while(true) {
try {
if(!client.isConnected())
client.connect(isa, 30000);

BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(client.getInputStream());

// send msg
out.write(data.getBytes());
out.flush();


System.out.println("Message sent, receiving return message...");


// get return msg
int length;
byte[] b = new byte[10240];

// read input stream
length = in.read(b);
retMsg = new String(b, 0, length);

System.out.println("Return Msg: " + retMsg);

Thread.sleep(60000);

} catch (java.io.IOException | InterruptedException e) {
System.out.println("Socket Error!");
System.out.println("IOException :" + e.toString());
}
}

最佳答案

try {
server = new ServerSocket("1234");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

不要写这样的代码。 catch block 应该位于末尾,所有依赖于 new ServerSocket 成功的代码都应该位于 try block 内。

synchronized (server) {
socket = server.accept();
}

这里不需要同步。

while (true) {
inData = new BufferedInputStream(socket.getInputStream());
outData = new BufferedOutputStream(socket.getOutputStream());

问题的很大一部分(如果不是全部)都在这里。每次围绕此循环,您都会不断创建新的缓冲流,这意味着之前的流缓冲的任何内容都将被丢弃。所以你正在失去输入。您应该在循环之前创建这两个流。

while(true) {
try {
if(!client.isConnected())
client.connect(isa, 30000);

这毫无意义。消除。您尚未展示如何创建客户端套接字,但如果您在未连接的情况下创建它,则应该在进入此循环之前连接它。

        BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(client.getInputStream());

这里您必须再次在循环之前创建这些流。

关于java - 服务器 : Socket hangs within unpredictable period time at read stream function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38320283/

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