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
socket_set_option
(PHP 4 >= 4.3.0, PHP 5)
socket_set_option — Modifie les options de socket
Description
socket_set_option() configure l'option spécifiée par optname , au niveau de protocole level à la valeur pointée par optval pour la socket spécifiée par socket .
Liste de paramètres
- socket
-
Une ressource de socket valide, créée par la fonction socket_create() ou la fonction socket_accept().
- level
-
Le paramètre level spécifie la couche du protocole de l'option. Par exemple, pour modifier une option de la couche socket, un niveau égal à SOL_SOCKET va être utilisé. Les autres niveaux, comme TCP, peuvent être utilisés en spécifiant un numéro de protocole pour ce niveau. Les numéros de protocoles peuvent être utilisés en utilisant la fonction getprotobyname().
- optname
-
Les options disponibles sont les mêmes que pour la fonction socket_get_option().
- optval
-
La valeur de l'option.
Valeurs de retour
Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.
Exemples
Exemple #1 Exemple avec socket_set_option()
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!is_resource($socket)) {
echo 'Impossible de créer la socket : '. socket_strerror(socket_last_error()) . PHP_EOL;
}
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo 'Impossible de définir l'option du socket : '. socket_strerror(socket_last_error()) . PHP_EOL;
}
if (!socket_bind($socket, '127.0.0.1', 1223)) {
echo 'Impossible de lier la socket : '. socket_strerror(socket_last_error()) . PHP_EOL;
}
$rval = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR);
if ($rval === false) {
echo 'Impossible de récupérer l'option du socket : '. socket_strerror(socket_last_error()) . PHP_EOL;
} else if ($rval !== 0) {
echo 'SO_REUSEADDR est défini sur la socket !' . PHP_EOL;
}
?>
Historique
| Version | Description |
|---|---|
| 4.3.0 | La fonction a été renommée. Elle s'appelait, avant, socket_setopt(). |
socket_set_option
27-Jun-2008 12:31
21-Dec-2006 10: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.
01-May-2005 02: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 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
17-May-2004 05:00
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
)
);
