CakeFest 2024: The Official CakePHP Conference

socket_set_block

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

socket_set_block设置套接字为阻塞模式

说明

socket_set_block(Socket $socket): bool

socket_set_block() 函数移除了由 socket 参数定义的 O_NONBLOCK 标记。

当一个操作(例如接收、发送、连接、接受连接……)在一个阻塞套接字上执行时,脚本在接受到信号或者可以执行此操作前将暂停执行。

参数

socket

socket_create()socket_accept() 创建的 Socket 实例。

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 现在 socketSocket 实例, 之前是 resource

示例

示例 #1 socket_set_block() 示例

<?php
$socket
= socket_create_listen(1223);
socket_set_block($socket);

socket_accept($socket);
?>

此示例在 1223 端口上创建了监听所有接口的套接字,并把此套接字设置为 O_BLOCK 模式。socket_accept() 将挂起,直到接受新的连接。

参见

add a note

User Contributed Notes 1 note

up
2
laacz at laacz dot lv
9 years ago
Besides true and false socket_set_block might return NULL if you're not too careful. That would happen when passing non socket resource as first parameter.

E.g. socket_set_block(false)) would return NULL and emit warning that you're trying to do things with non-socket.
To Top