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

search for in the

socket_shutdown> <socket_set_nonblock
Last updated: Fri, 13 Nov 2009

view this page in

socket_set_option

(PHP 4 >= 4.3.0, PHP 5)

socket_set_optionソケットのオプションを設定する

説明

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

socket_set_option() 関数は、level が指すプロトコルレベルでソケット socket のオプション optname に値 optval を設定します。

パラメータ

socket

socket_create() あるいは socket_accept() で作成したソケットリソース。

level

level パラメータは、オプションのプロトコルレベルを指定します。 例えば、オプションをソケットレベルで取得するには level パラメータに SOL_SOCKET を指定します。 TCP のようなそれ以外のレベルの場合、そのレベルのプロトコル番号を指定します。 プロトコル番号は getprotobyname() 関数を使用して取得可能です。

optname

使用可能なソケットオプションは socket_get_option() 関数と同じです。

optval

オプションの値。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 socket_set_option() の例

<?php
$socket 
socket_create(AF_INETSOCK_STREAMSOL_TCP);

if (!
is_resource($socket)) {
    echo 
'ソケットを作成できません: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!
socket_set_option($socketSOL_SOCKETSO_REUSEADDR1)) {
    echo 
'ソケットのオプションを設定できません: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!
socket_bind($socket'127.0.0.1'1223)) {
    echo 
'ソケットをバインドできません: 'socket_strerror(socket_last_error()) . PHP_EOL;
}

$rval socket_get_option($socketSOL_SOCKETSO_REUSEADDR);

if (
$rval === false) {
    echo 
'ソケットのオプションを取得できません: 'socket_strerror(socket_last_error()) . PHP_EOL;
} else if (
$rval !== 0) {
    echo 
'SO_REUSEADDR がソケットに設定されています!' PHP_EOL;
}
?>

変更履歴

バージョン 説明
4.3.0 関数の名前が変わりました。以前は socket_setopt() という名前でした。



socket_shutdown> <socket_set_nonblock
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
socket_set_option
aeolianmeson at ifacfchi dot blitzeclipse dot com
26-Jun-2008 10: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 09: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
01-May-2005 12: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
17-May-2004 03: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, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites