使用 PHP 发送 TCP 数据 [英] Sending TCP Data with PHP

查看:40
本文介绍了使用 PHP 发送 TCP 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've looked in to this a bit but still don't really understand if its possible.

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

I have a small webserver that sits on my private network. I want to create a php script that will connect to a telnet server on the private network and send it text every 30 seconds.

显然,最简单的部分是文本和计时,但由于某种原因,连接到 TCP 端口 23 并发送文本字符串似乎更难.这样做的最佳方法是什么?

Obviously the easy part is the text and timing but connection to a TCP Port 23 and sending a string of text seems harder for some reason. What's the best way to do this?

推荐答案

使用 PHP 套接字 :

Use PHP sockets :

<?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
    }
}
?>

这篇关于使用 PHP 发送 TCP 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆