socket_set_nonblock
(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
socket_set_nonblock — 设置套接字为非阻塞模式
说明
socket_set_nonblock() 为由 socket
参数指定的套接字设置 O_NONBLOCK
标记。
当一个操作(例如接收、发送、连接、接受连接……)在一个非阻塞套接字上执行时,脚本在接受到信号或操作处理完成前都不会暂停执行。如果操作会导致阻塞,被调用的函数将失败。
示例
示例 #1 socket_set_nonblock() 示例
<?php
$socket = socket_create_listen(1223);
socket_set_nonblock($socket);
socket_accept($socket);
?>
此示例在 1223 端口上创建了监听所有接口的套接字,并把此套接字设置为 O_NONBLOCK
模式。除非此时恰好有待处理的连接,否则 socket_accept() 将立即失败。
参见
- socket_set_block() - 设置套接字为阻塞模式
- socket_set_option() - 为套接字设置套接字选项
- stream_set_blocking() - 为资源流设置阻塞或者阻塞模式
+添加备注
用户贡献的备注 1 note
kpobococ at gmail dot com ¶
15 years ago
Beware, when using this function within a loop (i.e. a demon with a socket). The socket_accept(), for example, emits a warning each time there is no incoming connection available to be read. My php error log file got huge in a matter of seconds, eventually crashing the server.
Of course, i used the @ before the function to take care of that problem.
[EDITOR: One can (and should) use socket_select to detect a new connection on a socket (it's a "readable" event)]
备份地址:http://www.lvesu.com/blog/php/function.socket-set-nonblock.php