当套接字中的连接断开时会发生什么 (C)

发布于 2024-10-19 07:01:42 字数 642 浏览 0 评论 0原文

好吧,这似乎是一个相当简单的问题,但我觉得我已经阅读了我能找到的所有关于套接字编程的文章,但还没有找到令人满意的答案。首先让我描述一下我正在编程的系统。我很抱歉,但出于保密协议的目的,我必须非常含糊,但这足以让我的问题得到解答。

我正在编写一个带有线程池的中央多线程 C 服务器。有两种类型的客户,A 型和 B 型。每种客户都有数千个。 A 类是为 B 类做事的工作人员。A 类不断向服务器更新有关自身的信息(例如,每 15 秒)。类型 B 仅在需要完成某些操作时才与服务器对话,此时服务器会挑选一个 A 客户端并为其分配作业。该过程大约 24/7 持续进行,并且对时间非常敏感。

我决定采用持久 TCP 模型 - 这意味着只要 B 要求完成工作,服务器就可以立即将信息发送给 A,而无需等待相关 A 连接到服务器。此外,如果每个 A 每 15 秒与服务器对话一次,那么持续建立连接将会产生很大的开销。

如果服务器选择的A不可用,则需要尽快选择新的A,因为B很不耐烦。

我的问题是,如何判断连接是否已断开?我不是在谈论套接字被关闭,而是不再连接。例如,B1 希望完成工作,服务器选择 A1 并向其发送请求。然而,有人决定剪断以太网电缆。我无法让服务器愉快地向 A1 发送数据,直到几分钟后连接超时。我可以在尝试向客户端发送消息或其他内容之前对客户端执行 ping 操作吗?这会带来太多的延迟吗?可以做什么?

Okay, so this seems like a fairly straight forward issue but I feel that I've read every article on socket programming I can find and have not found a satisfactory answer. Let me first describe the system I am programming. I apologize, but I have to be very vague for NDA purposes, but it'll be enough to get my question across.

I'm writing a central multi threaded C server with a thread pool. There are two types of clients, type A and type B. There are thousands of each. Type A are workers which do things for Type B. Type A is constantly updating information about itself to the server (say, every 15 seconds). Type B only talks to the server when it needs something done, at which point the server picks out an A client and assigns it the job. This goes on roughly 24/7, and is very time sensitive.

I've decided to go with a persistent TCP model - this means as soon as B asks for work to be done, the server can immediately send the info over to A, without waiting for the A in question to connect to the server. Furthermore, if every A is talking to the server every 15 seconds, it would be a lot of overhead to keep establishing connections.

If the A chosen by the server is unavailable, it needs to select a new A as soon as possible because B is very impatient.

My question is, how do I tell if the connection has dropped? I'm not talking about a socket being closed, but just no longer connected. For example, B1 wants work done, server selects A1 and sends it the request. However, someone decides to snip the Ethernet cable. I can't afford to have the server happily sending data along to A1 until the connection times out minutes later. Can I ping the client before trying to send it messages or something? Will that introduce way too much latency? What can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-10-26 07:01:42

在我看来,唯一可靠的方法是使用计时器。如果回复时间过长,则假设已断开连接。将其放入要修复的服务器池中,并每隔一段时间检查一次以查看连接是否已恢复。

这基本上就是金融服务行业处理市场数据源的方式。如果您没有足够快地得到回复,您就不能再信任发件人,应该忽略它,直到情况有所改善。对于某些应用程序,他们甚至通过两个单独的网络路径(使用隧道、MPLS-TE、两个多播树)发送每个数据包的两个相同副本,以便在需要时始终有可用的备份数据包。

就您而言,您可能只需选择另一个工作人员并将任务发送给他们即可。

In my opinion the only reliable way to do this is with a timer. If it takes too long to reply, then assume that it is disconnected. Kick it into a pool of servers to be fixed, and check every once in a while to see if connectivity is restored.

This is basically how the financial services industry handles market data feeds. If you don't get a response quickly enough, you can no longer trust the sender and should ignore it until things improve. For some applications they even send two identical copies of every packet over two separate network paths (use tunnels, MPLS-TE, two multicast trees) so that they always have a backup packet available if they need it.

In your case, you can probably just choose another worker and send the task to them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文