gpt4 book ai didi

java - 尽管设置为超过两分钟,但套接字在两分钟后超时

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:53 25 4
gpt4 key购买 nike

正在关注 this post ,我遇到了同样的问题,我设法通过简单的测试转换重现了它。我希望你能帮助我。

让我解释一下,我正在使用套接字发送消息。只要我将 so_timeout 设置为小于两分钟,一切都运行良好。但是如果我将它设置为超过两分钟,则套接字会在两分钟后超时。因此,如果我将 so_timeout 设置为 10 秒,套接字将在 10 秒后超时,但如果我将其设置为 180 秒,套接字将在 120 秒后超时。

这是一个测试用例:

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
*
*/
public class TestSocket1 {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket();

serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(1111), 0);
serverSocket.setSoTimeout(1000);
Socket socket = null;

boolean send = true;

while (send) {
try {
socket = serverSocket.accept();
Thread.sleep(100);
String messageReceived = readFromSocket(socket);

System.out.println(messageReceived);

if (send) {
send = false;

Thread.sleep(150000); // Causing 2.5 minutes delay
// Sending message

BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
PrintWriter printWriter = new PrintWriter(wr, true);

String output = "Hello Back";

printWriter.println(output);
printWriter.flush();
socket.shutdownOutput();

}

}
catch (SocketTimeoutException ie) {
}
catch (Exception e) {
}
finally {
if (socket != null) {
socket.close();
}
}
}


}

protected static String readFromSocket(Socket socket) throws IOException {
StringBuilder messageReceived = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

String line = br.readLine();

messageReceived.append(line);

socket.shutdownInput();

return messageReceived.toString();
}


}

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/**
*
*/
public class TestSocket2 {

public static void main(String[] args) throws IOException {

Socket socket = new Socket();
socket.setKeepAlive(true);
socket.setReuseAddress(true);
socket.setTcpNoDelay(true);
socket.setSoTimeout(180000); // Should wait 3 minutes before throwing time out exception - Actually throwing after 2 minutes

SocketAddress socketAddress = new InetSocketAddress(1111);

socket.connect(socketAddress, 5000);

// Sending message

BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
PrintWriter printWriter = new PrintWriter(wr, true);

String output = "Hello There";

printWriter.println(output);
printWriter.flush();
socket.shutdownOutput();

String messageReceived = readFromSocket(socket);

System.out.println(messageReceived);

}

protected static String readFromSocket(Socket socket) throws IOException {
StringBuilder messageReceived = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

String line = br.readLine();

messageReceived.append(line);

socket.shutdownInput();

return messageReceived.toString();
}


}

您应该先运行 TestSocket1 类,然后再运行 TestSocket2。

我为这个问题苦苦挣扎了很长时间,我们将不胜感激。谢谢你。

  • 编辑

所以我删除了对 SO_TimeOut 的依赖并采纳了@Nick 在 this post 中的建议在读取之前检查可用的输入流。但现在的问题是,两分钟后可用字节总是返回 0,尽管输入已写入流。所以我仍然有同样的问题。

最佳答案

您不能将连接超时增加到超过平台默认值(大约一分钟)。你只能减少它。试图增加它实际上没有意义。你想要完成什么?

关于java - 尽管设置为超过两分钟,但套接字在两分钟后超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17007529/

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