socket_listen

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

socket_listenAttende una richiesta di connessione su un socket

Descrizione

socket_listen(resource $socket, int $backlog = ?): bool
Avviso

Questa funzione è SPERIMENTALE. Ovvero, il comportamento di questa funzione, il nome di questa funzione, in definitiva tutto ciò che è documentato qui può cambiare nei futuri rilasci del PHP senza preavviso. Siete avvisati, l'uso di questa funzione è a vostro rischio.

Una volta creato il socket socket tramite la funzione socket_create(), ed eseguito il bind ad un nome con socket_bind(), lo si può mettere in ascolto di eventuali richieste di connessione su socket.

Nota:

Il numero massimo, passato con il parametro backlog dipende fortemente dalla piattaforma sottostante. Su Linux questo viene troncato, senza avvisare, a SOMAXCONN. Su Win32, se viene passata la costante SOMAXCONN, il servizio sottostante responsabile dei socket valorizza backlog al massimo valore ragionevole. Non esiste un metodo standard per determinare il reale valore massimo su questa piattaforma.

La funzione socket_listen() è disponibile solo per i socket di tipo SOCK_STREAM o SOCK_SEQPACKET.

Restituisce true in caso di successo, false in caso di fallimento. Il codice di errore può essere recuperato con socket_last_error(). Questo codice può essere passato a socket_strerror() per ottenere una spiegazione dell'errore.

Vedere anche socket_accept(), socket_bind(), socket_connect(), socket_create() e socket_strerror().

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