Analysis of Socket connection and read and write data timeout issues in PHP

不言
Release: 2023-03-29 14:12:02
Original
1600 people have browsed it

This article mainly introduces the Socket connection and read and write data timeout issues in PHP, and analyzes the related settings and usage skills of Socket connection. Friends in need can refer to it

This article describes the Socket in PHP with examples Connection and reading and writing data timeout issues. Share it with everyone for your reference, the details are as follows:

Although PHP has a timeout parameter for connecting to the socket for the fsockopen() method, there is no timeout parameter setting for reading and writing data after a successful connection like in C. It doesn't matter, PHP provides a series of methods for stream to prevent timeout

stream_set_blocking( $fp , false )
Copy after login

Set the data stream to blocking mode to prevent it from exiting before the data is read

If the mode is false, the given socket descriptor will switch to non-block mode. If it is true, it will switch to block mode. This effect is similar to fgets() reading from the socket. In non-block mode fgets() will return immediately, while in block mode it will wait for the data to meet the requirements.

stream_set_timeout( $fp , 10 )
Copy after login

Set the timeout, which should be added immediately after the connection is successfully established. Sentence, the following parameter unit is seconds

stream_get_meta_data( $fp )
Copy after login

Get the header/metadata from the encapsulation protocol file pointer, and return an array, the format of which is:

Array
(
  [stream_type] => tcp_socket
  [mode] => r+
  [unread_bytes] => 0
  [seekable] =>
  [timed_out] =>
  [blocked] => 1
  [eof] =>
)
Copy after login

The index timed_out is the timeout information. If it times out, it is true. If it has not timed out, it is false. We can use this to judge whether the socket has timed out. What we need to pay attention to is This sentence should be added after each statement that needs to be waited for, such as fwrite(), fread(), and every time it is read, it must be judged whether it times out, and for a connection, only one timeout setting stream_set_timeout( $fp, 10 )

Code:

$fp = @fsockopen( $ip , $port, $errNo , $errstr, 30 );
if( !$fp )
{
  return false;
}
else
{
  stream_set_timeout( $fp , 3 ) ;
  //发送数据
  fwrite( $fp , $packet ) ;
  $status = stream_get_meta_data( $fp ) ;
  //发送数据超时
  if( $status['timed_out'] )
  {
    echo "Write time out" ;
    fclose( $fp );
    return false;
  }
  //读取数据
  $buf = fread( $fp , 16 ) ;
  $status = stream_get_meta_data( $fp ) ;
  //读取数据超时
  if( $status['timed_out'] )
  {
    echo "Read time out" ;
    fclose( $fp );
    return false;
  }
}
Copy after login

##Related recommendations:

PHP SCOKET sends large files, receiving end JAVA_PHP tutorial

The above is the detailed content of Analysis of Socket connection and read and write data timeout issues in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!