dsakktdog498483070 2013-12-11 14:33
浏览 55
已采纳

PHP多连接套接字(广播问题)

What I'm trying to achieve is a socket server that broadcasts some data to all connected peers. This is code of server loop:

while(TRUE) {
    $read = array();

    $read[] = $socket;

    for($i = 0; $i < $max_clients; $i++) {
        if($client_sockets[$i] != NULL) {
            $read[$i+1] = $client_sockets[$i];
        }
    }

    #This is broadcasting loop
    foreach($client_sockets as $send_sock)
    {
        socket_write($send_sock, "broadcasting".PHP_EOL);
    }

    if( socket_select($read, $write, $except, NULL) === FALSE ) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        die("Could not listen on socket : [$errorcode] $errormsg 
");
    }

    if( in_array($socket, $read) ) {
        for($i = 0; $i < $max_clients; $i++) {
            if($client_sockets[$i] == NULL) {
                $client_sockets[$i] = socket_accept($socket);

                if( socket_getpeername($client_sockets[$i], $client_address, $client_port) ) {
                    echo "Client $client_address : $client_port is now connected to us. 
";
                }

                $message = "Connected to php socket server".PHP_EOL;
                socket_write($client_sockets[$i], $message);
                break;
            }
        }
    }
}

This code works fine, accepts multiple connections and broadcasts data, except for one moment: loop starts only after I type any input from any connected client via telnet. I know this is because socket_select waits for this input, but I don't know how to start broadcasting right after client is connected.

Appreciate any help on this, because I've got feeling that I'm terribly wrong somewhere.

  • 写回答

1条回答 默认 最新

  • douyuqing_12345 2013-12-12 20:41
    关注

    Selfown answer

    Problem indeed was in socket_select($read, $write, $except, NULL); and not actually a problem. Last NULL parameter starts endless block, awaiting for client response; to make script work properly, I've changed this timeout, so it looks looks this: socket_select($read, $write, $except, 0, 500000). On the client side, however, parameter should be set to NULL(NOT 0!), so script remains idle while waiting for server broadcasting message.

    I've also discovered that such blocking behavior is similar to to socket_read() and socket_write() functions, which endless by default; to change it, use socket_set_option:

    socket_set_option($master_socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 0, "usec" => 1000));
    socket_set_option($master_socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 0, "usec" => 1000));
    

    And 1000 microseconds is as low as I could get.

    Actually, I've updated answer for somebody who voted up; but I hope that solution will be useful anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程