我想为远程控制台创建接口,需要使用第2页记录的自定义协议here。我创建了符合协议要求的正确类和数据包。问题是它工作一次(我工作的事情导致服务器返回数据包)现在每个请求都没有返回任何内容。我还应该提到的是,如果格式错误,服务器可以忽略数据包。我是否创建了与协议冲突的数据包,或者与此代码相关的数据包?
这是我的单词类:
class Word {
public $word;
public function __construct($word) {
$this->word = $word;
}
public function pack() {
return pack('Va*C', strlen($this->word), $this->word, 0);
}
}
这是我的包类:
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'word.class.php';
class Packet {
public $words = array();
protected $size = 0;
protected $is_request;
protected $from_client;
/**
* Construct packet
*
* @param bool $is_request
* @param bool $is_client
* @param string $command
*/
public function __construct($is_request, $from_client, $command) {
$this->is_request = $is_request;
$this->from_client = $from_client;
$this->size +=4 ; // size of first paremeter (4 bytes integer)
$command = trim(preg_replace('!\s+!', ' ', $command)); // remove double spaces
$this->size += strlen( str_ireplace(' ', '', $command) ) ; // size of words
$this->words = explode(' ',$command);
$this->size += count($this->words)*5; // add size of each word size + null byte (5 bytes for each word)
$this->size += 4; // add size of packet size
$this->size += 4; // size of words count
}
/**
* Return words packet string
*
* @return String
*/
protected function getWords(){
$result = '';
foreach($this->words AS $word) {
$word = new Word($word);
$result.= $word->pack();
}
return $result;
}
/**
* Return packet for sending
*
* @return string
*/
public function pack() {
return pack('C4VV', (int)$this->is_request, (int)$this->from_client, 0, 0, $this->size, count($this->words) ).$this->getWords();
}
public function getSize(){
return $this->size;
}
}
这是我的socket_read:
$bytes_received = socket_read($socket, 16384);
echo $bytes_received." bytes received.\n";
var_dump( unpack('C4VVa*', $response) );
我也尝试过这个和同样的结果:
$bytes_received = socket_recvfrom($socket, $response, 16384, 0, $address, $service_port);
当然我使用root帐户测试了php上的代码。