Timeout problem in [PHP] socket client

Time:2020-9-24

Connection socket is divided into connection timeout and read timeout

$sock=stream_ socket_ client(” www.google.com : 80 “, $errno, $errstr, 2); that number is the connection timeout. For example, if you connect to Google, an error will be returned in 2 seconds, so that you won’t be waiting there all the time
stream_ set_ Timeout ($sock, 5); this number is the timeout for reading data
 
stream_ get_ meta_ Data can return metadata in socket
 
For example, in the following test, because the server will be disconnected after the HTTP protocol is connected, it is not possible to use a long connection to transmit data all the time. You need to create a connection by constantly using new objects in the loop
for($i=0;$i<1000;$i++){
    $sock=("www.baidu.com:80", $errno,$errstr,2);  
    ($sock,5); 
    $meta=($sock);

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

    $buf = '';
    while (true) {
        $s = ($sock,1024);
        if (!isset($s[0])) {
            break;
        }
        $buf .= $s;
    }
    $meta=($sock);
    ("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)

The timed_ Out means the timeout of reading data, and false means that the reading has not timed out

EOF refers to whether it has reached the end of the file. If it is a long connection, it will not reach the end of the file. After the short connection of HTTP protocol is read, the connection will end