gpt4 book ai didi

php - socket_select 立即返回 false 但没有错误代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:07 25 4
gpt4 key购买 nike

我正在玩 socket_select,但是在一个主机上,这个函数做了一些奇怪的事情:

  • 立即返回,而不是等待 5 秒
  • 返回false,表示有错误
  • 但是 socket_last_error() 返回 0(成功)。
此服务器的

phpinfo():http://jsfiddle.net/Lmrfe/embedded/result/

$server = socket_create( AF_UNIX, SOCK_STREAM, 0 );
$r = socket_bind( $server, '/some/file/somewhere');
$r = socket_listen( $server );
// none of the above socket_* returns false

$t = microtime(true);
socket_clear_error();

$read = array( $server ); $write = null; $except = null;
$read = array( $server ); $write = array(); $except = array();
$read = array(); $write = array(); $except = array();
$read = null; $write = null; $except = null;

$changed = socket_select( $read, $write, $except, 5,0 );
$changed = socket_select( $read, $write, $except, null );
$changed = socket_select( $read, $write, $except, 5000000 );
$changed = socket_select( $read, $write, $except, 5000000, 0 );
$changed = socket_select( $read, $write, $except, 5000000, 5000000 );

/* Results:

microtime(true) - $t == almost zero

$changed === false

socket_last_error() === 0
socket_strerror(socket_last_error()) === Success

$read === array(1) {
[0]=>
resource(2) of type (Socket)
}
$write === NULL
$except === NULL
*/

$s = socket_read( $server, 1024, PHP_BINARY_READ );
// $s === false

这里发生了什么?

更新的测试脚本:仍然立即运行:

header('Content-Type: text/plain; charset=utf-8');
error_reporting(-1);

for( $i = 0; $i < 4; $i++ ){
for( $j = 0; $j < 5; $j++ ){
echo "\n\n\n\n\n[i,j]=[{$i},{$j}]\n";

$socket = socket_create( AF_UNIX, SOCK_STREAM, 0 );
var_dump('socket_create', $socket ); echo "\n"; if( $socket === false ) continue;

$socket_file = dirname(__FILE__)."/test_socket_i{$i}_j{$j}";
if( file_exists( $socket_file )) unlink( $socket_file );

$r = socket_bind( $socket, $socket_file );
var_dump('socket_bind', $r ); echo "\n"; if( $r === false ) continue;

$r = socket_listen( $socket );
var_dump('socket_listen', $r ); echo "\n"; if( $r === false ) continue;

$t = microtime(true);
socket_clear_error();

if($i==0){ $read = null; $write = null; $except = null; }
if($i==1){ $read = array(); $write = array(); $except = array(); }
if($i==2){ $read = array( $socket ); $write = null; $except = null; }
if($i==3){ $read = array( $socket ); $write = array(); $except = array(); }

if($j==0){ $changed = socket_select( $read, $write, $except, 5,0 ); } // 5 seconds
if($j==1){ $changed = socket_select( $read, $write, $except, null ); } // forever
if($j==2){ $changed = socket_select( $read, $write, $except, 5000000 ); }
if($j==3){ $changed = socket_select( $read, $write, $except, 5000000, 0 ); }
if($j==4){ $changed = socket_select( $read, $write, $except, 5000000, 5000000 ); }

var_dump('•socket_select returned:', $changed );echo "\n";
var_dump('•$read/$write/$except:',$read,$write,$except);echo "\n";
var_dump('•error:', socket_last_error(), socket_strerror(socket_last_error()) );echo "\n";
var_dump('time: ', microtime(true) - $t );echo "\n"; // almost zero
}}

最佳答案

您可能想使用 stream_* 函数而不是套接字。

Stream 函数更通用并且是 PHP 核心的一部分,而需要安装 Socket 支持。流函数基本上为您提供了更多控制权。

http://www.php.net/manual/en/intro.stream.php

关于php - socket_select 立即返回 false 但没有错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14773184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com