php socket_recvfrom多线程

php socket_recvfrom多线程,php,multithreading,Php,Multithreading,我在等待socket_recvfrom接收内容时遇到问题,否则脚本将无法继续运行 for( $i= 1024 ; $i <= 65535 ; $i++ ) { $sock[$i] = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_bind($sock[$i], $sourceips['madcoder'],$i); socket_connect($sock[$i], '115.188.58.144 ', 1195); $reques

我在等待socket_recvfrom接收内容时遇到问题,否则脚本将无法继续运行

for( $i= 1024 ; $i <= 65535 ; $i++ ) {
$sock[$i] = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($sock[$i], $sourceips['madcoder'],$i);
socket_connect($sock[$i], '115.188.58.144 ', 1195);
$request = 'data';
socket_write($sock[$i], $request);
$from = '';
$port = 0;
$valeur = socket_recvfrom($sock[$i], $buf, 1222, 0, $from, $port);
}
但问题是等待超过1秒的时间太多,并且将值设置为1秒以下存在问题

将值更改为

array('sec' => 0, 'usec' => 100)

行不通 所以我想到了多线程,我知道的实现多线程的唯一方法就是像我一样的“for”循环 但是我真的希望我能尝试其他更有效的方法,比如使用pthreads

<?php
class async_sender extends Thread {
public function __construct($i,$sourceip){
  $this->i=$i;
  $this->sourceip=$sourceip;
}
 public $sourceip;
 public $finished=false;
 public $response='';
 public $socket=NULL;
 public $i=-1;
public function run(){
    $this->socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_bind($this->socket, $this->sourceip,$this->i);
    socket_connect($this->socket, '115.188.58.144 ', 1195);
    $request = 'data';
    socket_write($this->socket, $request);
    $from = '';
    $port = 0;

    socket_recvfrom($this->socket, $this->response, 1222, 0, $from, $port);
    $this->finished=true;

}
}

$sender_threads=array();
for( $i= 1024 ; $i <= 65535 ; $i++ ) {
$sender_threads[$i]=new async_sender($i,$sourceips['madcoder']);
$sender_threads[$i]->start();
}
使用pthreads,,类似

<?php
class async_sender extends Thread {
public function __construct($i,$sourceip){
  $this->i=$i;
  $this->sourceip=$sourceip;
}
 public $sourceip;
 public $finished=false;
 public $response='';
 public $socket=NULL;
 public $i=-1;
public function run(){
    $this->socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_bind($this->socket, $this->sourceip,$this->i);
    socket_connect($this->socket, '115.188.58.144 ', 1195);
    $request = 'data';
    socket_write($this->socket, $request);
    $from = '';
    $port = 0;

    socket_recvfrom($this->socket, $this->response, 1222, 0, $from, $port);
    $this->finished=true;

}
}

$sender_threads=array();
for( $i= 1024 ; $i <= 65535 ; $i++ ) {
$sender_threads[$i]=new async_sender($i,$sourceips['madcoder']);
$sender_threads[$i]->start();
}

这只是通过使用不同的方法来避免问题,但它既不能解决问题,也不是一种解释。更好的答案应该包含解释和解决方案,包括
socket\u select()
或其他轮询机制,以及阻塞套接字和非阻塞套接字之间的区别。此外,pthreads仅适用于启用zend线程安全的PHP。这只能通过使用不同的方法来避免问题,但它既不能解决问题,也不能解释问题。更好的答案应该包含解释和解决方案,包括
socket\u select()
或其他轮询机制,以及阻塞套接字和非阻塞套接字之间的区别。此外,pthreads仅适用于启用zend线程安全的PHP。Read about and。Read about and。
<?php
class async_sender extends Thread {
public function __construct($i,$sourceip){
  $this->i=$i;
  $this->sourceip=$sourceip;
}
 public $sourceip;
 public $finished=false;
 public $response='';
 public $socket=NULL;
 public $i=-1;
public function run(){
    $this->socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_bind($this->socket, $this->sourceip,$this->i);
    socket_connect($this->socket, '115.188.58.144 ', 1195);
    $request = 'data';
    socket_write($this->socket, $request);
    $from = '';
    $port = 0;

    socket_recvfrom($this->socket, $this->response, 1222, 0, $from, $port);
    $this->finished=true;

}
}

$sender_threads=array();
for( $i= 1024 ; $i <= 65535 ; $i++ ) {
$sender_threads[$i]=new async_sender($i,$sourceips['madcoder']);
$sender_threads[$i]->start();
}