The Server Pages
:: TSP Menu
- Home
- Authors
- Glossary
- RSS Feed

:: Servers
- CPanel
- Hosting
- Linux
- Windows

:: Webmaster
- Databases
- Design
- Search Engines

:: Scripts
- Scripts

:: Documentation
- PHP Manual

:: Other
- Errors & Solutions
- TechPunt
- Wojjie

search for in the

socket_shutdown> <socket_set_nonblock
[edit] Last updated: Sat, 12 May 2012

view this page in


(PHP 4 >= 4.3.0, PHP 5)

socket_set_optionSets socket options for the socket


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

The socket_set_option() function sets the option specified by the optname parameter, at the specified protocol level, to the value pointed to by the optval parameter for the socket.



A valid socket resource created with socket_create() or socket_accept().


The level parameter specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function.


The available socket options are the same as those for the socket_get_option() function.


The option value.

Return Values

Returns TRUE on success or FALSE on failure.


Example #1 socket_set_option() example


if (!
is_resource($socket)) {
'Unable to create socket: 'socket_strerror(socket_last_error()) . PHP_EOL;

if (!
socket_set_option($socketSOL_SOCKETSO_REUSEADDR1)) {
'Unable to set option on socket: 'socket_strerror(socket_last_error()) . PHP_EOL;

if (!
socket_bind($socket''1223)) {
'Unable to bind socket: 'socket_strerror(socket_last_error()) . PHP_EOL;

$rval socket_get_option($socketSOL_SOCKETSO_REUSEADDR);

if (
$rval === false) {
'Unable to get socket option: 'socket_strerror(socket_last_error()) . PHP_EOL;
} else if (
$rval !== 0) {
'SO_REUSEADDR is set on socket !' PHP_EOL;


Version Description
4.3.0 This function was renamed. It used to be called socket_setopt().

socket_shutdown> <socket_set_nonblock
[edit] Last updated: Sat, 12 May 2012
add a note add a note User Contributed Notes socket_set_option
ckozler at kozler dot net 19-Mar-2012 07:06
It appears that Winsock does not acknowledge timeout (send and receive) on Windows.
DaveRandom 26-Nov-2010 04:13
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) {
aeolianmeson at ifacfchi dot blitzeclipse dot com 26-Jun-2008 03:31
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 21-Dec-2006 01:06
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.
drenintell 30-Apr-2005 05:19
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.

tim at e2-media dot co dot nz 16-May-2004 08:00
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

show source | credits | sitemap | contact | advertising | mirror sites