c - 用 C 检测网络事件

标签 c sockets networking signals

如果我失去与服务器的连接,我会启动警报并在 10 分钟内响起。与此同时,我尝试创建一个套接字并重新建立与服务器的连接。如果警报响起时,没有与服务器的连接,我想关闭应用程序。

检查套接字上是否存在实时连接的好方法是什么?我不确定阻塞方法是否可以接受(显然如果没有其他选择的话它们是可以接受的)。

最佳答案

If I lose connection to a server, I start an alarm to go off in 10 minutes.

所以那时你就知道没有任何联系。

In the meantime I try to create a socket and re-establish a connection to the server. If when the alarm goes off, there is no connection to the server, I want to close the application.

What would be a good way to go about checking if there is a live connection on a socket? I am unsure if blocking methods are acceptable (obviously if there is no alternative they are).

如果您在设置闹钟时知道没有连接,为什么在闹钟到期时不知道同样的事情?

在我看来,您所需要做的就是检查套接字 fd。如果它不为零,则表示有连接;如果它为零,则不会。并确保在设置闹钟时将其归零。

关于c - 用 C 检测网络事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18782198/

相关文章:

c# - NAudio AsioOut + socket

networking - 无需 NTP 的小尺寸时钟同步

c - stdout 上的 freopen_s 会导致 Windows 上的 GetConsoleScreenBufferInfo 出现问题

c++ - 为什么可变参数宏如此令人不快?

从原始位图数据创建 GdkPixbuf

c++ - 如何将本地端点绑定(bind)到套接字?

javascript - 服务器端事件 + 客户端事件与 Websocket

node.js - NodeBB 在 Nginx 反向代理上速度非常慢

git - 我可以强制 git 仅使用 TLS 吗?

c - 使用 malloc 函数查找空闲内存空间