tcp连接在TIME_WAIT将不允许重连,java [英] tcp connection in TIME_WAIT won't allow reconnect, java

查看:485
本文介绍了tcp连接在TIME_WAIT将不允许重连,java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与服务器建立tcp连接后,我关闭了我的 linux 应用程序,并调用了Socket.close()。

After making a tcp connection to a server, I close my linux application and Socket.close() is called.

检查netstat -pant,我看到连接处于TIME_WAIT状态。

Checking netstat -pant, I see the connection is in TIME_WAIT status.

这阻止我立即连接回服务器,因为我使用相同的端口连接。相反,我必须等待连接超时的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.

我玩过 - 没有运气 - 使用套接字方法: set_so_timeout(),set_keepalive(),set_so_linger()和set_reuseaddr() - 方法的正确拼写可能不正确在这篇文章。

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.

我的问题是我将连接退出TIME_WAIT状态,以便我可以立即重新建立连接?

My question is HOW do I get the connection out of the TIME_WAIT status so I can instantly make a connection again?

请让我知道。

感谢,
jbu

Thanks, jbu

推荐答案

将连接从TIME_WAIT中取出的最好方法wait: - )

The best way to get the connection out of TIME_WAIT is (surprisingly) to wait :-)

这就是TCP / IP的工作原理。会话由元组(sourceIP,sourcePort,destIP,destPort,protocol)标识,并且您不能重复使用它的原因是因为可能有数据包它仍然出现在网络的某个地方。

That's how TCP/IP works. 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.

TIME_WAIT状态通常是最大数据包生命周期的两倍,你应该不会导致数据包从上一个会话中显示出来(这会使您当前的会话变坏)。

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).

理想情况下,您应该从不同的源端口连接,立即打开会话。

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.

这篇关于tcp连接在TIME_WAIT将不允许重连,java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆