设置socket_read超时

发布于 2024-07-10 14:24:23 字数 326 浏览 5 评论 0原文

我想知道如何为 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 技术交流群。

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

发布评论

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

评论(4

老娘不死你永远是小三 2024-07-17 14:24:23

这设置了套接字的 5 秒超时。

socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>5, "usec"=>0));

this set 5 sec timeout of the socket.

socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>5, "usec"=>0));
愁杀 2024-07-17 14:24:23

您是否尝试过 socket_set_optionSO_RCVTIMEO

输入操作的超时值。

Have you tried socket_set_option with SO_RCVTIMEO

Timeout value for input operations.

花辞树 2024-07-17 14:24:23

我做了一个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.

唔猫 2024-07-17 14:24:23
$read_socket = socket_select($read, $write  = NULL, $except = NULL, 10); // 10 - Timeout
if($read_socket === FALSE)
    $this->End();
elseif($read_socket === 0)
    return FALSE;

$pdu_ = socket_read($this->session, 102400);
if($read_socket && !strlen($pdu_))
    $this->End();
$read_socket = socket_select($read, $write  = NULL, $except = NULL, 10); // 10 - Timeout
if($read_socket === FALSE)
    $this->End();
elseif($read_socket === 0)
    return FALSE;

$pdu_ = socket_read($this->session, 102400);
if($read_socket && !strlen($pdu_))
    $this->End();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文