PHp fsockopen()通过Telnet进行通信

时间:2011-08-18 19:38:56

标签: php telnet fsockopen

我正在寻找有关如何使用fsockopen()与telnet系统进行通信的指南....我连接得很好,但命令无法发送。我看过一些文档fwrite(),显示人们发送了一些标题。

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

1 个答案:

答案 0 :(得分:0)

显然我需要做的就是确保在命令结束时加入\n !!