如果我失去与服务器的连接,我会启动警报并在 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/