我正在构建一个多线程PHP CLI应用程序,它通过套接字与服务器通信。目的是让应用程序仅与服务器创建一个连接(通过单独的类),然后允许子线程使用已建立的套接字对象。以下代码失败:
<?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_stackoverflow/test.sh on line 35
PHP Stack trace:
PHP 1. {main}() /test_stackoverflow/test.sh:0
PHP 2. socket->request() /test_stackoverflow/test.sh:17
PHP 3. fwrite() /test_stackoverflow/test.sh:35
PHP Warning: fgets() expects parameter 1 to be resource, integer given in /test_stackoverflow/test.sh on line 36
PHP Stack trace:
PHP 1. {main}() /test_stackoverflow/test.sh:0
PHP 2. socket->request() /test_stackoverflow/test.sh:17
PHP 3. fgets() /test_stackoverflow/test.sh:36
如果我删除了Thread方面,请使用以下代码:
<?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();
?>
此代码成功运行。
似乎通过引用传递$ server对象(套接字类)来扩展Thread的类会导致问题。
我想知道是否有办法实现我最初的目标,即构建一个通过单个套接字连接与服务器通信的多线程PHP CLI应用程序。
感谢您的协助!
最终的应用程序将更加健壮,允许动态生成线程。我上面提供的示例是显示根本问题,而不是其他任何内容。
答案 0 :(得分:1)
我更新了套接字类声明以扩展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();
?>