PHP: socket_set_option - Manual

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, 26 Jan 2007

view this page in

# socket_set_option

(PHP 4 >= 4.3.0, PHP 5)

socket_set_option -- Sets socket options for the socket

## Description

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

The socket_set_option() function sets the option specified by the `optname` parameter, at the protocol level specified by the `level` parameter, to the value pointed to by the `optval` parameter for the socket specified by the `socket` parameter. socket_set_option() will return `FALSE` on failure.

The `level` parameter specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, a `level` parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function.

The available socket options are the same as those for the socket_get_option() function.

> Замечание: This function used to be called socket_setopt() prior to PHP 4.3.0.

add a note User Contributed Notes
socket_set_option
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
)
);`
18-Jun-2002 10: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);`
add a note
socket_shutdown socket_set_nonblock

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST