PHP 个共享中央套接字对象的线程

PHP Threads Sharing a Central Socket Object

我正在构建一个多线程 PHP CLI 应用程序,它通过套接字与服务器通信。目的是让应用程序仅创建一个与服务器的连接(通过单独的 class),然后允许子线程使用已建立的套接字对象。以下代码失败:

<?php

/**
 * Test child class
 **/
class test extends Thread {

    private $server;

    public function __construct( &$server ) {
        $this->server = $server;
    }

    public function run() {
        echo $this->server->request( 'INFO 1-10' );
    }

}


/**
 * Socket class
 **/
class socket {

    private $socket;

    public function __construct( $host, $port ) {
        $this->socket = fsockopen( $host, $port, $errno, $errstr, 10 );
    }

    public function request( $out ) {
        fwrite( $this->socket, $out . "\r\n" );
        return fgets( $this->socket );
    }

}


/**
 * main class
 **/
class main {

    private $server;

    public function __construct() {
        $this->server = new socket( '192.168.1.141', 5250 );
    }

    public function main() {
        $test = new test( $this->server );
        $test->start();
    }

}

$main = new main();
$main->main();

?>

错误信息是:

PHP Warning:  fwrite() expects parameter 1 to be resource, integer given in /test_Whosebug/test.sh on line 35
PHP Stack trace:
PHP   1. {main}() /test_Whosebug/test.sh:0
PHP   2. socket->request() /test_Whosebug/test.sh:17
PHP   3. fwrite() /test_Whosebug/test.sh:35
PHP Warning:  fgets() expects parameter 1 to be resource, integer given in /test_Whosebug/test.sh on line 36
PHP Stack trace:
PHP   1. {main}() /test_Whosebug/test.sh:0
PHP   2. socket->request() /test_Whosebug/test.sh:17
PHP   3. fgets() /test_Whosebug/test.sh:36

如果我删除线程方面,并使用以下代码:

<?php

/**
 * Test child class
 **/
class test {

    private $server;

    public function __construct( &$server ) {
        $this->server = $server;
    }

    public function run() {
        echo $this->server->request( 'INFO 1-10' );
    }

}


/**
 * Socket class
 **/
class socket {

    private $socket;

    public function __construct( $host, $port ) {
        $this->socket = fsockopen( $host, $port, $errno, $errstr, 10 );
    }

    public function request( $out ) {
        fwrite( $this->socket, $out . "\r\n" );
        return fgets( $this->socket );
    }

}


/**
 * main class
 **/
class main {

    private $server;

    public function __construct() {
        $this->server = new socket( '192.168.1.141', 5250 );
    }

    public function main() {
        $test = new test( $this->server );
        $test->run();
    }

}

$main = new main();
$main->main();

?>

此代码运行成功。

似乎是通过引用扩展线程的 class 传递 $server 对象(套接字 class)导致了问题。

我想知道是否有一种方法可以实现我最初的目标,即构建一个多线程 PHP CLI 应用程序,该应用程序通过单个套接字连接与服务器通信。

感谢您的帮助!

最终的应用程序将更加健壮,允许动态生成线程。我上面提供的示例是为了显示根本问题,仅此而已。

我更新了套接字 class 声明以扩展 Threaded class,现在它工作正常。

<?php

/**
 * Test child class
 **/
class test extends Threaded {

    private $server;

    public function __construct( &$server ) {
        $this->server = $server;
    }

    public function run() {
        echo $this->server->request( 'INFO 1-10' );
    }

}


/**
 * Socket class
 **/
class socket extends Threaded {

    private $socket;

    public function __construct( $host, $port ) {
        $this->socket = fsockopen( $host, $port, $errno, $errstr, 10 );
    }

    public function request( $out ) {
        fwrite( $this->socket, $out . "\r\n" );
        return fgets( $this->socket );
    }

}


/**
 * main class
 **/
class main {

    private $server;

    public function __construct() {
        $this->server = new socket( '192.168.1.141', 5250 );
    }

    public function main() {
        $test = new test( $this->server );
        $test->run();
    }

}

$main = new main();
$main->main();

?>