PHp fsockopen()通过Telnet通信


PHp fsockopen() communicating via Telnet

我正在寻找如何使用fsockopen()与telnet系统....通信的指南我的连接正常,但是命令发送失败。我已经看到一些文档fwrite()显示人们发送一些头。

目前我对telnet服务器运行的命令是version通过$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);
}

显然,我所需要做的就是确保在命令末尾包含一个'n !!