我正在寻找有关如何使用fsockopen()与 telnet 系统进行通信的指南……我连接得很好,但是命令发送失败。我看过一些文档fwrite()
显示人们发送了一些标头。
目前我对 telnet 服务器运行的命令是version
via $class->send("version");
。我是否需要发送标头或其他任何内容以供 telnet 服务器接收命令,或者我可以只发送吗?
/**
* Connect to the GMC telnet system
*/
public function connect () {
$this->connection = fsockopen($this->socket['host'], $this->socket['port'], $errorNumber, $errorMessage, 30);
if (!$this->connection) {
$this->error = 'Unable to connect to GMC: '.$errorMessage.' ('.$errorNumber.')';
return false;
}
stream_set_timeout($this->connection, $this->commandTimeout);
return true;
}
/**
* Send a command to GMC
*/
public function send ($command) {
//write to socket
if (fwrite($this->connection, $command) === false) {
$this->error = 'Unable to write to socket';
return false;
}
sleep(1);
//read socket
if (($response = fgets($this->connection)) === false) {
$this->error = 'Unable to write to socket';
return false;
}
return $response;
}
/**
* Disconnects from the GMC telnet system
*/
public function disconnect () {
return fclose($this->connection);
}