socket_listen
(PHP 4 >= 4.1.0, PHP 5)
socket_listen — Abre escuta para uma conexão no socket
Descrição
Esta função é EXPERIMENTAL. O comportamento desta função, seu nome, incluindo toda documentação pode ser modificado sem aviso em futuras versões do PHP. Esta função deve ser usada por sua própria conta e risco.
Após o socket socket ter sido criado usando socket_create() e associado para um nome com socket_bind(), ele deve dizer para aguardar por escuta em conexões que irão entrar socket.
Um máximo de backlog em conexões entrantes irão ser listadas para este processamento. Se uma requisição de conexão chegar com um limite de clientes devem receber um erro com uma identificação de ECONNREFUSED, ou, se o protocolo abaixo suporta retransmissão, a requisição deve ser ignorada de forma até serem bem sucedidas.
Nota:
O máximo número passado para o parâmetro backlog altamente dependente da plataforma abaixo. No linux, isso é silenciosamente truncado para SOMAXCONN. No win32, se passado SOMAXCONN, o disponibilizador de serviço abaixo responsável pelo socket irá configurar o backlog para o valor máximo reasonable. Não há uma condição para procurar o valor do backlog atual nesta plataforma.
socket_listen() é aplicável somente para sockets do tipo SOCK_STREAM ou SOCK_SEQPACKET.
Retorna TRUE em caso de sucesso ou FALSE em falhas. O código de erro pode ser retornado com socket_last_error(). Isso código deve ser passado para socket_strerror() para pegar uma explicação textual do erro.
Veja também socket_accept(), socket_bind(), socket_connect(), socket_create() e socket_strerror().
User Contributed Notes
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.
socket_listen() cannot be used for UDP communications as discussed below.
In addition, the example below discusses UDP connections, which only exist if the application manages them through a state table (the OS does not create a UDP connection upon receiving a datagram). Having a function named socket_connect() that determines the remote IP and port only confuses the matter by giving the indication of some sort of connection handshake between two hosts. Rather, socket_connect() only specifies the remote IP and port used by subsequent socket_send() calls. You can achieve the same effect by skipping socket_connect() altogether and specifying the remote IP and port in socket_sendto() calls.
If you find yourself writing a connection-based protocol on top of UDP, consider using TCP. If your application requires streaming data of some sort, use TCP to manage connections and control messages, and UDP to handle the streaming data (H.323 is an example of a suite of TCP and UDP protocols working in conjunction).