Timeout issues when [PHP] socket client

And socket connector into connection timeout Read Timeout

$ Sock = stream_socket_client ( "www.google.com:80", $ errno, $ errstr, 2); that number is a connection timeout, such as connecting google, 2 seconds returns an error, so that would not have been waiting in the
stream_set_timeout ($ sock, 5); this number is read timeout data
 
stream_get_meta_data may return metadata in the socket
 
For example, the following test, because http protocol connection will be cut off completely the service side, we had to use the long connection has been transmission of data, need to keep in the loop new objects to create a connection
for($i=0;$i<1000;$i++){
    $sock=stream_socket_client("www.baidu.com:80", $errno,$errstr,2);  
    stream_set_timeout($sock,5); 
    $meta=stream_get_meta_data($sock);

    var_dump("start",$meta);
    fwrite($sock, "GET / HTTP/1.0\r\n\r\n");

    $buf = '';
    while (true) {
        $s = fread($sock,1024);
        if (!isset($s[0])) {
            break;
        }
        $buf .= $s;
    }
    $meta=stream_get_meta_data($sock);
    var_dump("end",$meta,$sock);
}
string(5) "start"
array(7) {
  ["stream_type"]=>
  string(14) "tcp_socket/ssl"
  ["mode"]=>
  string(2) "r+"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
}
string(3) "end"
array(7) {
  ["stream_type"]=>
  string(14) "tcp_socket/ssl"
  ["mode"]=>
  string(2) "r+"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(true)
}
resource(175) of type (stream)

Timed_out which data is read-out, false overtime not to read

eof as to whether the end of the file, if it is a long connection here is not going to reach the end of the file, http protocol will read this short connection after the connection is over

 

Guess you like

Origin www.cnblogs.com/taoshihan/p/12053187.html