PHP 8.3.4 Released!

stream_socket_accept

(PHP 5, PHP 7, PHP 8)

stream_socket_acceptAceita uma conexão em um socket criado por stream_socket_server()

Descrição

stream_socket_accept(resource $socket, ?float $timeout = null, string &$peer_name = null): resource|false

Aceita uma conexão em um socket previamente criado por stream_socket_server().

Parâmetros

socket

O socket do servidor de onde a conexão será aceita.

timeout

Substitui o limite de tempo padrão de aceitação do socket. O tempo deve ser informado em segundos. Por padrão, default_socket_timeout é usado.

peer_name

Será definido para o nome (endereço) do cliente que conectou, se incluído e disponível no transporte selecionado.

Nota:

Pode também ser determinado mais tarde usando stream_socket_get_name().

Valor Retornado

Retorna um fluxo para a conexão de socket aceita ou false em caso de falha.

Registro de Alterações

Versão Descrição
8.0.0 timeout agora pode ser nulo.

Notas

Aviso

Esta função não deve ser usada com sockets de servidor UDP. No lugar dela, use stream_socket_recvfrom() e stream_socket_sendto().

Veja Também

add a note

User Contributed Notes 5 notes

up
7
mickael dot bailly at free dot fr
17 years ago
this function, compared to the function socket_accept, got an extra argument "timeout".
To make this function wait indefinitelly to incoming connections, just as in socket_accept, set timeout to -1. It works for me with PHP 5.0.4.
up
5
Andy at txtNation dot com
12 years ago
To check if there's a new connection waiting, without blocking, or (when using non-blocking mode) without notices), you can use stream_accept (as opposed to socket_select).

<?php

class GenericClass {

protected
$resSocket=null;

function
acceptConnections() {

# check that we still have a resource

if(is_resource($this->resSocket)) {

$arrRead=array($this->resSocket);

$arrWrite=array();

/** @warning Passing $arrRead,$arrWrite by reference */
if(stream_select($arrRead,$arrWrite,$arrWrite,0)) {

$resConnection=stream_socket_accept($this->resSocket,0);

# ... other stuff here
}
}
}
}
?>
up
6
leleu256NOSPAM at hotmail dot com
19 years ago
This code could be very helpfull...

The following code is for the "server". It listen for a message until CTRL-C

<?php
while (true)
{
// disconnected every 5 seconds...
receive_message('127.0.0.1','85',5);
}

function
receive_message($ipServer,$portNumber,$nbSecondsIdle)
{
// creating the socket...
$socket = stream_socket_server('tcp://'.$ipServer.':'.$portNumber, $errno, $errstr);
if (!
$socket)
{
echo
"$errstr ($errno)<br />\n";
}
else
{
// while there is connection, i'll receive it... if I didn't receive a message within $nbSecondsIdle seconds, the following function will stop.
while ($conn = @stream_socket_accept($socket,$nbSecondsIdle))
{
$message= fread($conn, 1024);
echo
'I have received that : '.$message;
fputs ($conn, "OK\n");
fclose ($conn);
}
fclose($socket);
}
}
?>

The following code is for the "client". It send a message, and read the respons...

<?php

send_message
('127.0.0.1','85','Message to send...');

function
send_message($ipServer,$portServer,$message)
{
$fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);
if (!
$fp)
{
echo
"ERREUR : $errno - $errstr<br />\n";
}
else
{
fwrite($fp,"$message\n");
$response = fread($fp, 4);
if (
$response != "OK\n")
{echo
'The command couldn\'t be executed...\ncause :'.$response;}
else
{echo
'Execution successfull...';}
fclose($fp);
}
}
?>
up
1
Alex Nordenheim
9 years ago
Note that if you use 0 as timeout, the connection will timeout right away.
up
0
fred dot hakeem dot smith at muslimamerica dot bob dot net
16 years ago
To whom it may concern, and it may concern you greatly, stream_set_blocking has no effect on stream_socket_accept.
If you want it to return right away, connection or not, use 0 for the timeout parameter.
To Top