设置socket_read超时
我想知道如何为 socket_read
调用设置超时? 第一次调用 socket_read
时,它会等待直到发送数据,如果 5 秒内没有发送数据,我想关闭连接。 有帮助吗? 我已经尝试过 SO_RCVTIMEO
但没有成功。
我正在使用 socket_create()
创建一个套接字并监听它的连接,然后当连接时我监听数据,然后用它做一些事情。 当超时时,我想运行 socket_shutdown()
然后运行 socket_close()
。
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.
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()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这设置了套接字的 5 秒超时。
this set 5 sec timeout of the socket.
您是否尝试过 socket_set_option 与 SO_RCVTIMEO
输入操作的超时值。
Have you tried socket_set_option with SO_RCVTIMEO
Timeout value for input operations.
我做了一个socket_listen,然后我用 time()+2 和一个带有非块集和socket_read() 的 while 循环进行了手动超时。 似乎工作正常。 还有其他选择吗?
更新:我发现将套接字设置为非阻塞,然后使用 socket_listen 提供了我需要的超时。
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?
UPDATE: I found that setting the socket as nonblocking and then using socket_listen provided the timeout I needed.