socket_set_option

(PHP 4 >= 4.3.0, PHP 5)

socket_set_optionEstablece las opciones de socket para el socket

Descripción

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

La función socket_set_option() establece la opción especificada por el parámetro optname, al nivel (level) del protocolo especificado, al valor apuntado por el parámetro optval para el socket.

Parámetros

socket

Un recurso socket válido creado con socket_create() o socket_accept().

level

El parámetro level especifica el nivel de protocolo en el que reside la opción. Por ejemplo, para recuperar opciones a nivel de socket, se debería usar un parámetro level de SOL_SOCKET. Otros niveles, como TCP, se pueden usar para especificar el número de protocolo de ese nivel. Los números de protocolo se pueden encontrar usando la función getprotobyname().

optname

Las opciones de socket dispobibles son las mismas que las de la función socket_get_option().

optval

El valor opcional.

Valores devueltos

Devuelve TRUE en caso de éxito o FALSE en caso de error.

Ejemplos

Ejemplo #1 Un ejemplo de socket_set_option()

<?php
$socket 
socket_create(AF_INETSOCK_STREAMSOL_TCP);

if (!
is_resource($socket)) {
    echo 
'No se pudo crear elsocket: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!
socket_set_option($socketSOL_SOCKETSO_REUSEADDR1)) {
    echo 
'No se pudo establecer la opción del socket: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!
socket_bind($socket'127.0.0.1'1223)) {
    echo 
'No se pudo vincular el socket: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

$rval socket_get_option($socketSOL_SOCKETSO_REUSEADDR);

if (
$rval === false) {
    echo 
'No se pudo obtener la opción del option: 'socket_strerror(socket_last_error()) . PHP_EOL;
} else if (
$rval !== 0) {
    echo 
'SO_REUSEADDR está establecido en el socket !' PHP_EOL;
}
?>

add a note add a note

User Contributed Notes 6 notes

up
2
drenintell
9 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 http://ca.php.net/manual/en/ref.sockets.php for the available Predefind Constants and visit http://man.he.net/man2/setsockopt 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.

Regards,
  drenintell
up
1
DaveRandom
4 years ago
Setting the socket timeout microseconds ('usec') does not work under Windows, at least under PHP/5.2.9:

<?php

  $timeout
= array('sec'=>1,'usec'=>500000);
 
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);
 
var_dump(socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO));

?>

Output on Windows box:

array(2) {
  ["sec"]=>
  int(1)
  ["usec"]=>
  int(0)
}

Output on Linux box:

array(2) {
  ["sec"]=>
  int(1)
  ["usec"]=>
  int(500000)
}
up
0
ckozler at kozler dot net
2 years ago
It appears that Winsock does not acknowledge timeout (send and receive) on Windows.
up
0
aeolianmeson at ifacfchi dot blitzeclipse dot com
6 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
up
0
ludvig dot ericson at gmail dot com
8 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.
up
0
tim at e2-media dot co dot nz
10 years ago
To set a socket timeout value (assuming you've set it blocking) use:

socket_set_option(
  $socket,
  SOL_SOCKET,  // socket level
  SO_SNDTIMEO, // timeout option
  array(
    "sec"=>10, // Timeout in seconds
    "usec"=>0  // I assume timeout in microseconds
    )
  );
To Top