dongwen3093 2012-04-23 16:36
浏览 95
已采纳

Socket_select:它会影响它的输入,但究竟如何呢?

In the php documentation listed here there's an indication that the function uses the read, error and write array values as refference but I have failed to find anywhere what is actually changed in those values.

I wrote a simple 'server' I use now and I noted that when the mentioned function fails to see any data on the polled clients, then it changes the array and renders it unusable to poll again. This is my code:

<php - all socket creation binding and listening above..
socket_set_nonblock($mysock);

$w = $e = array();
$clients = array($mysock);

do {
    $temp_client = @socket_accept($mysock);
    if ($temp_client != FALSE) 
        $clients[] = $temp_client;

    $clients_backup = $clients;

    $select_socket = socket_select($clients,$w,$e,0);
    if (($select_socket !== FALSE) AND ($select_socket != 0)) {
        foreach ($clients as $value) {
            $input = socket_read($value,1024,PHP_BINARY_READ);
            echo $input;
        }
    }
    $clients = $clients_backup;
} while(TRUE);

When testing this consider that the first connection is not handled.. I have no idea why (would very much thank elightment) this code is a strip of what I'm developing and there it works ok. The socket_select issue remains the same in both codes.

Basically If I do not make that backup of the clients array, it doesn't work. The error: "no resource arrays were passed to select" is seen since the array doesn't have the socket resource after socket_select first checks a connection with no data to read.

  • 写回答

1条回答 默认 最新

  • duanmangxie7131 2012-04-23 17:31
    关注

    These arrays are arrays of socket resources. socket_select() will modify the arrays by removing from them any resources that aren't ready to be read from, written to, etc., respectively.

    You'll almost always want to pass it copies of your arrays, because they will be modified, and you don't want to lose your original references to them:

    $clients = [ ... ];
    $r = $w = $e = $clients;
    
    $numChanged = socket_select($r, $w, $e, ...);
    
    // Here, $r, $w, and $e will only contain the resources that are ready for processing, which may be none at all
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示