如何绕过超时时间,立即关闭套接字?

发布于 2024-08-20 05:10:21 字数 125 浏览 1 评论 0原文

在Java中,当你关闭一个socket时,它不会再做任何事情,但它实际上会在超时时间后关闭TCP连接。

我需要使用数千个套接字,并且我希望它们在关闭后立即关闭,而不是在超时之后关闭,这浪费了我的时间和资源。我能做些什么?

In Java, when you close a socket, it doesn't do anything anymore, but it actually closes the TCP connection after a timeout period.

I need to use thousands of sockets and I want them to be closed immediately after I close them, not after the timeout period, which wastes my time and my resources. What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情愿 2024-08-27 05:10:21

我发现通过使用socket.setReuseAddress(boolean),您可以告诉JVM重用该端口,即使它处于超时期间。

I found out that by using socket.setReuseAddress(boolean), you can tell the JVM to reuse the port even if it's in the timeout period.

つ可否回来 2024-08-27 05:10:21

您可能会看到套接字处于 TIME_WAIT 状态。这是套接字在执行“主动关闭”的连接一侧进入的正常状态。 TIME_WAIT 的存在是有充分理由的,因此您应该小心地重复使用地址。

我在我的博客上写了关于TIME_WAIT、它存在的原因以及在编写服务器时可以采取的措施:http://www.serverframework.com/asynchronousevents/2011/01/time-wait -and-its-design-implications-for-protocols-and-scalable-servers.html

总之,如果可以的话,请更改协议,以便您的客户端进入TIME_WAIT

You are probably seeing sockets in TIME_WAIT state. This is the normal state for a socket to enter on the side of the connection that does the 'active close'. TIME_WAIT exists for a very good reason and so you should be careful of simply reusing addresses.

I wrote about TIME_WAIT, why it exists and what you can do about it when writing servers here on my blog: http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html

In summary, if you can, change the protocol so that your clients enter TIME_WAIT.

女皇必胜 2024-08-27 05:10:21

如果您正在运行服务器,则 ServerSocket 是正确的解决方案。通过回收和一系列旨在使用 Java 运行服务器的其他优化,它会比您手动管理一切更好。

关闭套接字会断开 Java 对象与操作系统的连接,这意味着它不会占用 JVM 之外的任何资源,因此这确实不应该成为问题。但是,如果 Java 的垃圾收集/终结方案的最小开销太大,那么 Java 就不是一个有效的解决方案(因为您的问题不再特定于套接字编程)。尽管我不得不说,高效的垃圾收集器并不比显式管理内存差多少(而且实际上可以表现得更好)。

If you're running a server, then ServerSocket is the proper solution. It will manage everything better than you doing it by hand through recycling and a host of other optimizations intended for running a server with Java.

Closing the socket disconnects the Java object from the operating system, which means that it isn't taking up any resources outside of the JVM, so it really shouldn't be a problem. But if the minimal overhead from Java's garbage collection/finalization scheme is too big of a burden, then Java isn't a valid solution (since your problem isn't specific to socket programming any more). Although I have to say that an efficient garbage collector is not much worse than explicitly managing memory (and can actually perform better).

人生戏 2024-08-27 05:10:21

“我希望它们在关闭后立即关闭,而不是在浪费我的时间和资源之后关闭!”
不,你不知道。您希望 TCP/IP 正常工作,而 TIME_WAIT 状态是其中至关重要的一部分。如果您担心 TIME_WAIT 状态,快速的答案是成为接收FIN 的人,而不是第一个发送FIN 的人。

'I want them to be closed exactly after closed them not after wasting my time and my resources!'
No you don't. You want TCP/IP to work correctly, and the TIME_WAIT state is a critically important part of that. If you're worried about the TIME_WAIT state, the quick answer is to be the one who receives the FIN rather than the one who first sends it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文