简体   繁体   English

TIME_WAIT中的TCP连接将不允许重新连接,Java

[英]tcp connection in TIME_WAIT won't allow reconnect, java

After making a tcp connection to a server, I close my linux application and Socket.close() is called. 与服务器建立TCP连接后,我关闭了Linux应用程序,并调用了Socket.close()。

Checking netstat -pant, I see the connection is in TIME_WAIT status. 检查netstat -pant,我看到连接处于TIME_WAIT状态。

This prevents me from making an immediate connection back to the server since I'm using the same port to connect from. 因为使用相同的端口进行连接,所以这阻止了我立即建立与服务器的连接。 Instead, I have to wait for the connection to timeout of the TIME_WAIT status before I can reconnect again. 相反,我必须等待连接超时到TIME_WAIT状态,然后才能再次重新连接。

I've played around -without luck- with the socket methods: set_so_timeout(), set_keepalive(), set_so_linger(), and set_reuseaddr() - exact spelling of the method may not be right in this post. 我玩过-没有运气-使用套接字方法:set_so_timeout(),set_keepalive(),set_so_linger()和set_reuseaddr()-该方法的确切拼写在本文中可能不正确。

My question is HOW do I get the connection out of the TIME_WAIT status so I can instantly make a connection again? 我的问题是如何使连接脱离TIME_WAIT状态,以便立即重新建立连接?

Please let me know. 请告诉我。

Thanks, jbu 谢谢jbu

The best way to get the connection out of TIME_WAIT is (surprisingly) to wait :-) 使连接脱离TIME_WAIT的最佳方法是(令人惊讶地)等待:-)

That's how TCP/IP works. 这就是TCP / IP的工作方式。 A session is identified by the tuple (sourceIP, sourcePort, destIP, destPort, protocol) and the reason why you can't re-use it is because there may be packets for it still out in the network somewhere. 会话由元组(sourceIP, sourcePort, destIP, destPort, protocol)之所以不能重复使用,是因为网络中某些地方可能仍存在一些数据包。

TIME_WAIT state is generally twice the maximum packet lifetme and you should not be fiddling with it since that may cause packets to show up from the previous session (which will screw up your current session). TIME_WAIT状态通常是最大数据包寿命的两倍,因此您应该摆弄它,因为这可能会导致数据包从上一个会话中显示(这会破坏当前会话)。

Ideally, you should connect from a different source port, then you will be able to open the session immediately. 理想情况下,您应该从其他源端口进行连接,然后便可以立即打开会话。

Another thing you should watch out for is badly closed sessions. 您还需要注意的另一件事是严重关闭的会话。 I've always subscribed to the guideline that the client should shut down the session (and shut it down cleanly). 我一直都遵循客户端应该关闭会话(并干净地关闭它)的准则。 This minimizes the possiblity for long-lived half-closed sessions hanging around. 这样可以最大程度地减少长期闲置的半封闭会议的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM