Home > Article > Backend Development > socket 设置超时问题
<?phpfunction Send_socket_connect($service_port, $address, $in) { //创建 TCP/IP socket $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("could not create socket!"); //创建连接 $result = socket_connect($socket, $address, $service_port) or die("could not connect with" . $address); //发送命令到设备 socket_write($socket, $in, strlen($in)); //接受返回值 $buffer = socket_read($socket, 1024, PHP_NORMAL_READ); //关闭连接 socket_close($socket); return $buffer;}$str = Send_socket_connect(5000, "10.0.1.22", "[AddDev].2.0.信号3.admin.12345.10.0.1.28.8000");echo $str;?>
连接需要20多秒
手册上没有吗?
设置$socket 发送超时1秒,接收超时3秒:
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1, "usec"=>0 ) );
socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>3, "usec"=>0 ) );
设置$socket 发送超时1秒,接收超时3秒:
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1, "usec"=>0 ) );
socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>3, "usec"=>0 ) );
socket_connect($socket, $address, $service_port) or die("could not connect with" . $address); 一般都是连接超时的,连接超时怎么办??