java 如何绕过超时期限立即关闭套接字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2187889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:55:47  来源:igfitidea点击:

How can I make a socket close immediately, bypassing the timeout period?

javasocketstimeout

提问by Shayan

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

在 Java 中,当您关闭套接字时,它不再执行任何操作,但实际上会在超时时间后关闭 TCP 连接。

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?

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

采纳答案by Shayan

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.

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

回答by Len Holgate

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

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

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

我写了关于TIME_WAIT它为什么存在以及在我的博客上编写服务器时你可以做些什么: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.

总之,如果可以,请更改协议,以便您的客户输入TIME_WAIT.

回答by Travis Gockel

If you're running a server, then ServerSocketis 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.

如果您正在运行服务器,那么ServerSocket是合适的解决方案。它将通过回收和许多其他用于使用 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 minimaloverhead 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).

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

回答by user207421

'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 receivesthe FIN rather than the one who first sendsit.

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