PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

socket_shutdown> <socket_set_nonblock
Last updated: Fri, 10 Oct 2008

view this page in

socket_set_option

(PHP 4 >= 4.3.0, PHP 5)

socket_set_optionSetzt die Optionen für einen Socket

Beschreibung

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

Mit socket_set_option() wird die Option, die mit optname angegeben wurde, auf der Protokollebene level für den Socket socket auf den Wert gesetzt, der in optval übergeben wurde.

Parameter-Liste

socket

Ein gültiger Socket-Deskriptor, der von socket_create() oder socket_accept() erzeugt wurde.

level

Der Parameter level gibt die Protokollebene an, auf der diese Option gilt. Um beispielsweise Optionen auf dem Socket-Level abzufragen würde man den Parameter level auf den Wert SOL_SOCKET setzen. Andere Ebenen, wie etwa TCP können benutzt werden, indem man die Protokollnummer dieser Ebene benutzt. Protokollnummern erhält man mit der Funktion getprotobyname().

optname

Es sind dieselben Socket-Optionen verfügbar, wie bei der Funktion socket_get_option() beschrieben.

optval

Der Wert der Option.

Rückgabewerte

Gibt bei Erfolg TRUE zurück, im Fehlerfall FALSE.

Beispiele

Beispiel #1 socket_set_option() Beispiel

<?php
$socket 
socket_create(AF_INETSOCK_STREAMSOL_TCP);

if (!
is_resource($socket)) {
    echo 
'Kann keinen Socket erzeugen: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!
socket_set_option($socketSOL_SOCKETSO_REUSEADDR1)) {
    echo 
'Kann keine Option setzen für Socket: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!
socket_bind($socket'127.0.0.1'1223)) {
    echo 
'Kann den Socket nicht benennen: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

$rval socket_get_option($socketSOL_SOCKETSO_REUSEADDR);

if (
$rval === false) {
    echo 
'Kann keine Socket-Optionen ermitteln: 'socket_strerror(socket_last_error()) . PHP_EOL;
} else if (
$rval !== 0) {
    echo 
'SO_REUSEADDR ist für den Socket gesetzt!' PHP_EOL;
}
?>

ChangeLog

Version Beschreibung
4.3.0 Diese Funktion wurde umbenannt. Ihr bisheriger Name war socket_setopt().



socket_shutdown> <socket_set_nonblock
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
socket_set_option
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 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
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:

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
    )
  );

socket_shutdown> <socket_set_nonblock
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites