socket_listen

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

socket_listen监听套接字的连接

说明

socket_listen(Socket $socket, int $backlog = 0): bool

socket_create() 创建套接字 socket 并通过 socket_bind() 绑定名称后,可以监听 socket 收到的连接。

socket_listen() 仅适用于 SOCK_STREAMSOCK_SEQPACKET 类型的套接字。

参数

socket

socket_create()socket_addrinfo_bind() 创建的套接字实例。

backlog

backlog 指定处理连接请求队列的最大值。如果一个连接请求到达时队列已满,客户端可能会收到 ECONNREFUSED 的错误提示。若底层协议支持重传,则忽略该请求,以便重试成功。。

注意:

传递给 backlog 参数的最大值取决于底层平台。Linux 中,超过最大值将默认截取为 SOMAXCONN。win32 中,如果超过 SOMAXCONN 的值,负责套接字的底层服务将把 backlog 设置为最大的 reasonable 合理值,在此平台上,没有提供可以找到 backlog 实际值的标准描述。

返回值

成功时返回 true, 或者在失败时返回 false。 可以通过 socket_last_error() 来检索错误码。将错误码作为参数传递给 socket_strerror() 以获得错误的文本解释。

更新日志

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

参见

add a note add a note

User Contributed Notes 2 notes

up
4
renmengyang567 at gmail dot com
4 years ago
<?php
// create for tcp
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_bind($sock, '127.0.0.1',5000);
socket_listen($sock,1);
sleep(20);
?>

<fruit>
netstat  -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:5000          0.0.0.0:*               LISTEN      1839/php
up
-34
Karuna Govind (karuna.kgx gmail)
15 years ago
To change the maximum allowed backlog by your system (*nix machines only), first you need to find the variable for this limit:

sudo sysctl -a | grep somaxconn

On ubuntu boxes, it returns net.core.somaxconn (you need to look for the 'somaxconn' variable, the full name will vary across different systems).

Update this to a large number as follows:

sudo sysctl -w net.core.somaxconn=1024

This will work straight away. no restart required.
To Top