问题描述
我正在构建一个多线程的PHP CLI应用程序,该应用程序通过套接字与服务器通信.目的是使应用程序仅与服务器建立一个连接(通过单独的类),然后允许子线程使用已建立的套接字对象.以下代码失败:
I am building a multi-threaded PHP CLI application that speaks with a server via sockets. The intention is for the application to only create one connection with the server (via a separate class) and then allow the child threads to use the established socket object. The following code fails:
<?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方面,并使用以下代码:
If I remove the Thread aspect, and use the following code:
<?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的类会导致问题.
It appears that passing the $server object (socket class) by reference to a class that extends Thread causes the issue.
我想知道是否有一种方法可以实现我最初的目标,即构建一个多线程PHP CLI应用程序,该应用程序通过单个套接字连接与服务器通信.
I was wondering if there was a way to accomplish my original goal of building a multi-threaded PHP CLI application that speaks with a server via a single sockets connection.
感谢您的协助!
最终的应用程序将更加健壮,允许动态生成线程.我上面提供的示例仅用于显示根本问题.
推荐答案
我更新了套接字类声明,以扩展线程类,现在可以正常工作了.
I updated the socket class declaration to extend the Threaded class, and now it works fine.
<?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();
?>
这篇关于共享中央套接字对象的PHP线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!