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

socket_set_optionValorizza le opzioni per un socket


socket_set_option ( resource $socket , int $level , int $optname , mixed $optval ) : bool

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.

La funzione socket_set_option() imposta l'opzione specificata dal parametro optname, per il livello di protocollo indicato da level sul socket indicato da socket, al valore indicato dal parametro optval. La funzione socket_set_option() restituisce false se si verifica un errore.

Il parametro level indica il livello di protocollo nel quale si trova l'opzione. Ad esempio, per ageire sulle opzioni a livello di socket, occorre impostare il parametro level a SOL_SOCKET. Si possono utlizzare livelli, tipo TCP, semplicemente impostando il valore di protocollo nel parametro level. I valori previsti possono essere recuperati tramite la funzione getprotobyname().

Le oopzioni disponibili per i socket sono le medesime indicate per la funzione socket_get_option().


Nelle versioni di PHP antecedenti la 4.3.0, questa funzione era chiamata socket_setopt().

add a note add a note

User Contributed Notes 8 notes

16 years ago
To expand a bit more on what "tim at e2-media dot co dot nz" started.

SO_SNDTIMEO is one of the many constants you can use with socket_set_option.

See for the available Predefind Constants and visit for the meaning of the ones relevant.

Tim's example might seem at first a bit non-intuitive since he is using the SO_SNDTIMEO constant. Which means, if the socket has to send out data, it must do it within the limit specified - in his case 10 seconds. Usually you won't set a timeout for sending out data. Nevertheless, the example is valid, and there are situations where you need to do so.

A more intuitive use of socket_set_option would be to set a time out for a blocking socket (a socket that waits for data to be receive when read from). You would do this like so:

socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>0, "usec"=>100));

Notice that sec= 0 and usec= 100; Depending on how long you want your program to wait to recieve data, you might want to change these values.

gmail user asmqb7
1 year ago

PHP 7.3.6, and probably many previous versions, automatically sets SO_REUSEADDR when you use stream_socket_server().

php_network_bind_socket_to_local_addr() is called at and defined at and if you take a look you'll see

            setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&sockoptval, sizeof(sockoptval));

I initially thought I'd need to play with context options to turn this on, but no, the simplest single-arg call with no error checking and just an address, works for me.

strace your PHP binary to be 100% sure:

setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0

The chances are you ARE using SO_REUSEADDR unless you're using a 100-year old UNIX clone in a month with a Z in it.
renmengyang567 at gmail dot com
2 years ago
Why is the size of the buffer 2 times that set by me?
//Before setting the cache area
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_bind($sock, '',5000);
$sndbuf = socket_get_option($sock,SOL_SOCKET,SO_SNDBUF);
$rcvbuf = socket_get_option($sock,SOL_SOCKET,SO_RCVBUF);
printf("send buffer size(写缓存区大小):%sm \n",$sndbuf/1024);
printf("receive buffer(读缓存区大小)%sm \n",$rcvbuf/1024);

//After setting the cache area
$snd_buf = 1024*3;
$rcv_buf = 1024*3;

socket_set_option($sock,SOL_SOCKET,SO_SNDBUF, $snd_buf);
socket_set_option($sock,SOL_SOCKET,SO_RCVBUF, $rcv_buf);
$sndbuf = socket_get_option($sock,SOL_SOCKET,SO_SNDBUF);
$rcvbuf = socket_get_option($sock,SOL_SOCKET,SO_RCVBUF);

printf("send buffer size(写缓存区大小):%sm \n",$sndbuf/1024);
printf("receive buffer size(读缓存区大小)%sm \n",$rcvbuf/1024);
aeolianmeson at ifacfchi dot blitzeclipse dot com
12 years ago
Lingering will sometimes not work when you're working with non-blocking sockets. Even if the socket is set to linger and you keep tying to close until the socket doesn't return an error and the resource is no longer identifiable as type 'Socket', the socket may STILL close without sending everything.

Therefore, in the event that you are using non-blocking sockets (which is preferable if you care at all about signaling), you should set the socket as blocking (socket_set_block()) before calling to close it. This will allow everything to flush before it returns.

Dustin Oprea
ludvig dot ericson at gmail dot com
14 years ago
I would like to comment on the previous note regarding blocking sockets.
There is more to blocking sockets than waiting for data to be received when trying to be read upon, just to make example, a listening blocking socket will wait for a client to try to connect before it returns when you socket_accept() it.
tim at e2-media dot co dot nz
17 years ago
To set a socket timeout value (assuming you've set it blocking) use:

  SOL_SOCKET,  // socket level
  SO_SNDTIMEO, // timeout option
    "sec"=>10, // Timeout in seconds
    "usec"=>0  // I assume timeout in microseconds
10 years ago
Setting the socket timeout microseconds ('usec') does not work under Windows, at least under PHP/5.2.9:


= array('sec'=>1,'usec'=>500000);


Output on Windows box:

array(2) {

Output on Linux box:

array(2) {
ckozler at kozler dot net
9 years ago
It appears that Winsock does not acknowledge timeout (send and receive) on Windows.
To Top