Home  >  Article  >  Backend Development  >  socket 设置超时问题

socket 设置超时问题

WBOY
WBOYOriginal
2016-06-23 14:38:111920browse

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



ip不能连通的话,要超过 20s 才会返回不能连接  ;
如何设置   连接超时,发送超时,返回超时

新人不知道分数怎么用,还请各位不吝赐教!!谢谢


回复讨论(解决方案)

   连接需要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);  一般都是连接超时的,连接超时怎么办??

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn