使用 Telnet 调试 TCP

发布于 2023-12-12 14:15:17 字数 3298 浏览 34 评论 0

像 redis,beanstalk,memcached 都是基于 TCP 的,有的时候需要直接通过 TCP 调试与传输数据,比如编写客户端插件,使用 telnet 可以很方便地进行调试。

调试 redis:

% telnet localhost 6379                                                                                                                            
Trying ::1...
Connected to localhost.
Escape character is '^]'.

然后可以直接交互式执行命令了:

% telnet localhost 6379                                                                                                                            
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set a hello
+OK
get a
$5
hello

编写客户端就是建立 socket 连接,然后把这些命令发送过去,然后解析返回的数据而已。

<?php
/**
 * Created by human.
 * User: Weinan Tang <twn39@163.com>
 * Date: 2018-12-29
 * Time: 15:36
 */

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '0.0.0.0');
$result = socket_connect($socket, '121.199.44.30', 11300);


function get_line($socket) {
    $buffer = '';
    $result = '';

    while ($buffer !== "\n") {
        $buffer = socket_read($socket, 1024, PHP_NORMAL_READ);
        $result .= $buffer;
    }

    $head = explode(' ', trim($result));
    return $head;
}

function get_body($socket, $length) {

    $buffer = socket_read($socket, $length + strlen("\r\n"), PHP_BINARY_READ);
    return $buffer;
}

function execute($command, $socket) {
    socket_write($socket, $command."\r\n");

    $head = get_line($socket);

    $body = '';
    if ($head[0] === strtoupper('reserved')) {
        $body = get_body($socket, $head[2]);
    }

    $content = [
        'head' => $head,
        'body' => $body,
    ];
    var_dump($content);

    return $content;

}

socket_set_block($socket);

$result = execute('use feed', $socket);
$result = execute('watch feed', $socket);
$result = execute('ignore default', $socket);

while (true) {
    $result = execute('reserve', $socket);
}
<?php
class Sockets
{
    private $socket;

    private $host;

    private $port;

    public function __construct($host, $port)
    {
        $this->host = $host;
        $this->port = $port;

        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_connect($this->socket, $this->host, $this->port);
    }

    public function execute($command)
    {
        socket_write($this->socket, $command."\r\n");
        $head = $this->getLine();
        $body = '';

        if ($this->hasBody($head[0])) {
            $length = strtolower($head[0]) === 'ok' ? $head[1]:$head[2];
            $body = $this->getBody($length);
        }

        return [
            'head' => $head,
            'body' => $body,
        ];
    }

    public function hasBody($keyword)
    {
        $longCommand = ['reserved', 'ok'];
        return in_array(strtolower($keyword), $longCommand);
    }

    public function getLine() {
        $buffer = '';
        $result = '';

        while ($buffer !== "\n") {
            $buffer = socket_read($this->socket, 1024, PHP_NORMAL_READ);
            $result .= $buffer;
        }

        return explode(' ', trim($result));
    }

    public function getBody($length) {

        return socket_read($this->socket, $length + strlen("\r\n"), PHP_BINARY_READ);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

家住魔仙堡

暂无简介

文章
评论
26 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文