使用 PHP 发送 TCP 数据

Sending TCP Data with PHP

我已经对此进行了一些研究,但仍然不太明白它是否可行。

我有一个小型网络服务器,位于我的专用网络上。我想创建一个 php 脚本,它将连接到专用网络上的 telnet 服务器并每 30 秒向它发送一次文本。

显然,简单的部分是文本和时间,但由于某些原因,连接到 TCP 端口 23 和发送一串文本似乎更难。执行此操作的最佳方法是什么?

使用 PHP 个套接字:

<?php
while(true){
    sleep 30;
    $fp = fsockopen("www.example.com", 23, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, "The string you want to send");
        while (fgets($fp, 128)) {
            echo fgets($fp, 128); // If you expect an answer
        }
        fclose($fp); // To close the connection
    }
}
?>