jjzjj

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

coder 2024-04-12 原文

我正在玩 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/

有关php - socket_select 立即返回 false 但没有错误代码的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  3. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  4. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  5. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  6. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  7. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  8. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  9. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  10. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

随机推荐