连接中断短时间后 TCP 连接被拒绝
我正在使用 (Java) TCP 套接字连接到支持网络的设备。
考虑以下场景:
1) TCP 套接字连接成功创建
2) 网络连接短暂中断(我只是通过拔掉网络电缆来测试这一点)
3) 我的程序关闭了套接字。
4)对方的socket只响应我这边的请求;因此,连接另一端的套接字不一定检测到断开的连接
5) 重新建立网络连接
6) 我的程序尝试再次打开套接字
7) ->得到一个 SocketException: connection returned
为什么?
因为另一端的套接字认为 tcp 连接仍然打开,因此拒绝同一端口上的任何其他连接请求。
接下来会发生什么? 。
大约 5 分钟后,我的程序能够再次打开套接字,因为另一端检测到连接不再处于活动状态
问题:
有什么方法可以缩短我再次重新连接的时间吗?我无法在“其他”站点上进行任何更改,即我无法更改启用网络的设备的 TCP 连接处理。
I'm using a (Java) TCP socket to connect to a network-enabled device.
Think about the following scenario:
1) TCP socket connection successfully created
2) Network connection interrupted for a short time (I'm testing this by simply unplugging the network cable)
3) My program closes the socket.
4) The other side of the socket only responds to requests from my side; thus, the socket at the other end of the connection does not necessarily detect the broken connection
5) Network connection is reestablished
6) My program tries to open the socket again
7) -> Get a SocketException: connection refused
Why?
Because the socket at the other end thinks that the tcp connection is still open and thus refuses any other connection requests at the same port.
What happens next?
After about 5 minutes, my program is able to open the socket again, because the other end detected that the connection is not active anymore.
Question:
Is there any way to reduce the time until I'm able to reconnect again? I'm not able to make any changes on the "other" site, i.e., I can't change the tcp connection handling of my network enabled device.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果无法更改另一端处理 TCP 连接的方式,不幸的是,在这种情况下您无能为力。这是常见 问题一般 Java 套接字。
Without being able to change how the TCP connection is handled on the other side, there is unfortunately nothing much you can do in this scenario. This is a common issue with Java sockets in general.