php socket函数socket_set_option怎么用?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php socket函数socket_set_option怎么用?相关的知识,希望对你有一定的参考价值。

php函数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 specified protocollevel, to the value pointed to by the optval parameter for the socket。
参数说明:
socket
A valid socket resource created with socket_create() or socket_accept().
level
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 thegetprotobyname() function.
optname
The available socket options are the same as those for the socket_get_option() function.
optval
The option value.
返回值:

成功时返回 TRUE, 或者在失败时返回 FALSE。
例子:
<?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;

?>
参考技术A 设置socket选项。
bool socket_set_option ( resource $socket , int $level , int $optname , mixed $optval )
参数是需要设置的socket,协议等级,调用socket_get_option()的名称和值追问

谢谢,能说具体点吗,就像socket_create是创建套接字的,这个字面上看不明白

追答

在socket_create()之后,设置一些参数,用于socket_get_option()来取的
mixed socket_get_option ( resource $socket , int $level , int $optname )

本回答被提问者和网友采纳

以上是关于php socket函数socket_set_option怎么用?的主要内容,如果未能解决你的问题,请参考以下文章

php socket函数socket_set_option怎么用?

phpwebsocket - 未定义函数 socket_create() - 在 Windows/WAMP 上启用 php_sockets

PHP Socket 编程之9个主要函数的使用之测试案例

PHP socket连接

PHP Socket 编程进阶指南

PHP Socket编程 之使用fsockopen()函数