在socket_read上设置超时 [英] Set a timeout on socket_read
问题描述
我想知道如何在socket_read
呼叫上设置超时?第一次调用socket_read
时,它将等待直到发送数据为止,如果5秒钟内没有发送数据,我想关闭连接.有帮助吗?我已经尝试SO_RCVTIMEO
没有运气了.
I was wondering how can I set a timeout on a socket_read
call? The first time it calls socket_read
, it waits till data is sent, and if no data is sent within 5 secs I want to shutdown the connection. Any Help? I already tried SO_RCVTIMEO
with no luck.
我正在使用socket_create()
创建一个套接字,并在其上侦听连接,然后在连接时侦听数据,然后对其进行处理.当超时达到时,我要先运行socket_shutdown()
然后运行socket_close()
.
I'm creating a socket with socket_create()
and listening on it for connections, then when connected I listen for the data and then do something with it. When the timeout hits, I want to run socket_shutdown()
and then socket_close()
.
推荐答案
我做了一个socket_listen,然后使用time()+ 2进行了手动超时,并在其中设置了非块集和socket_read()进行了while循环.似乎工作正常.有其他选择吗?
I did a socket_listen and then I made a manual timeout with time()+2 and a while loop with nonblock set and socket_read() inside. Seems to be working ok. Any alternatives?
更新:我发现将套接字设置为非阻塞状态,然后使用socket_listen提供了我需要的超时时间.
UPDATE: I found that setting the socket as nonblocking and then using socket_listen provided the timeout I needed.
这篇关于在socket_read上设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!