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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码