gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:04:35 26 4
gpt4 key购买 nike

我正在构建一个通过套接字与服务器通信的多线程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();

?>

此代码成功运行。

似乎通过引用扩展Thread的类传递$ server对象(socket类)会导致此问题。

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

谢谢你的协助!

最终的应用程序将更加健壮,从而可以动态生成线程。我上面提供的示例仅用于显示根本问题,而没有其他内容。

最佳答案

我更新了套接字类声明以扩展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();

?>

关于php - 共享中央套接字对象的PHP线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com