在线手册的用户注释区其实有很多有用的参考,你可以在 http://php.net/manual/en/function.socket-get-option.php 里搜索 timeout,因为这个问题很普遍,所以肯定会有。 如果你懒的动手,摘录一段代码给你SO_SNDTIMEO Reports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value. 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 ) );
如果你懒的动手,摘录一段代码给你SO_SNDTIMEO Reports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value.
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
)
);
1,socket可写,那么getsockopt SO_ERROR获取错误,没错误则连接完成。
2,select超时返回,那么关闭socket。因为PHP是C写的,接口和源生API基本一样,变变名字而已:1,dio_fcntl 是 fcntl的别名,用来设置非阻塞, 但是在socket目录下已经直接提供了用来修改socket阻塞/非阻塞的接口:
socket_ set_ block
socket_ set_ nonblock
2,socket的相关函数你已经使用了。
3,select也是有php实现的,socket_select
4,getsockopt我查了之后,也是有实现的,上面同学也用到了:socket_get_option