Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Socket Functions : socket_set_option

socket_set_option

Sets socket options for the socket (PHP 4 >= 4.3.0, PHP 5)
bool socket_set_option ( resource socket, int level, int optname, mixed optval )

Example 2303. socket_set_option() example

<?php
$socket
= socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

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

if (!
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
   echo
'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}

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

$rval = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR);

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

Code Examples / Notes » socket_set_option

tim

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


drenintell

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


ludvig dot ericson

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.


19-jun-2002 06:35

Example:
I am not sure if the fourth argument (optval) is used in SO_REUSEADDR ... I don't think it is...   In that case, this is how you set your socket re-usable:
socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,1);


Change Language


Follow Navioo On Twitter
socket_accept
socket_bind
socket_clear_error
socket_close
socket_connect
socket_create_listen
socket_create_pair
socket_create
socket_get_option
socket_getpeername
socket_getsockname
socket_last_error
socket_listen
socket_read
socket_recv
socket_recvfrom
socket_select
socket_send
socket_sendto
socket_set_block
socket_set_nonblock
socket_set_option
socket_shutdown
socket_strerror
socket_write
eXTReMe Tracker