Tcp 套接字关闭 [英] Tcp Socket Closed

查看:37
本文介绍了Tcp 套接字关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为,如果不实现心跳,就无法知道 TCP 连接的一侧是否意外死亡.如果进程只是在一侧被杀死并且没有正常退出,则套接字无法发送 FIN 或让另一侧知道它已关闭.

I always thought that if you didn't implement a heartbeat, there was no way to know if one side of a TCP connection died unexpectedly. If the process was just killed on one side and didn't exit gracefully, there was no way for the socket to send FIN or let the other side know that it was closed.

(参见此处的一些评论,例如 http://www.perlmonks.org/?node_id=566568 )

(See some of the comments here for example http://www.perlmonks.org/?node_id=566568 )

但是有一个我连接到的股票订单服务器,它有一个新的在断开连接时取消所有订单功能",如果客户端断开连接,它会取消实时订单.即使我在我的最后终止进程,它也能工作,而且我的应用程序绝对没有心跳.

But there is a stock order server that I connect to that has a new "cancel all orders on disconnect feature" that cancels live orders if the client dis-connects. It works even when I kill the process on my end, and there is definitely no heartbeat from my app to it.

那么它如何能够检测到我何时终止了进程?我的应用程序在 Windows Server 2003 上运行,订单服务器在 Suse Linux Enterprise Server 10 上.Windows 是否检测到与套接字关联的进程不再活动并发送 FIN?

So how is it able to detect when I've killed the process? My app is running on Windows Server 2003 and the order server is on Suse Linux Enterprise Server 10. Does Windows detect that the process associated with the socket is no longer alive and send the FIN?

推荐答案

当进程退出时 - 无论出于何种原因 - 操作系统将关闭它已打开的 TCP 连接.

When a process exits - for whatever reason - the OS will close the TCP connections it had open.

TCP 连接还有很多其他方法可以在未被检测到的情况下失效

There's numerous other ways a TCP connection can go dead undetected

  • 有人拔掉了中间的网线.
  • 另一端的计算机被炸毁了.
  • 中间的 nat 网关会悄悄断开连接
  • 另一端的操作系统严重崩溃.
  • FIN 数据包丢失.

尽管启用了 tcp keepalive,您最终还是会检测到它 - 至少在几个小时内.

Though enabling tcp keepalive, you'll detect it eventually - atleast during a couple of hours.

这篇关于Tcp 套接字关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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