如何使用 PHP 将数据通过 UDP 发送到 IP 地址?
如何使用 PHP 将数据通过 UDP 发送到 IP 地址?
How can I send data with PHP to an IP address via UDP?
如何在另一台计算机上接收该数据?
How can I recive that data on the other computer?
<?php
$fp = pfsockopen( "udp://192.168.1.6", 9601, $errno, $errstr );
if (!$fp)
{
echo "ERROR: $errno - $errstr<br />
";
}
socket_set_timeout ($fp, 10);
$write = fwrite( $fp, "kik" );
//$data .= fread($fp,9600);
//echo "$data<br>";
fclose($fp);
echo "<br>Connection closed ..<br>";
if (!$write) {
echo "error writing to port: 9600.<br/>";
next;
?>
此代码发送带有程序的kik",我可以在另一台计算机上阅读它,但我如何在浏览器中看到它?
This code sends the "kik" with a program I can read it on the another computer, but how can I see it in the browser?
推荐答案
我的 PHP 知识有点生疏,所以我一直在寻找一些好的指南和教程.这个PHP Sockets Made Easy看起来将是一个很好的入门指南你.
My PHP knowledge is a bit rusty so I've been doing some searching trying to find some good guides and tutorials. This one PHP Sockets Made Easylooks like it will be a good starter guide for you.
编辑:我发布的原始文章没有详细介绍 UDP,因此我删除了前面的代码.PHP 手册 中的文章有一些具体关于UDP:
Edit: The original article I posted did not go into great detail for UDP so I eliminated the previous code. The article from the PHP Manual has some more information specifically regarding UDP:
<?php
$socket = stream_socket_server("udp://127.0.0.1:1113", $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) {
die("$errstr ($errno)");
}
do {
$pkt = stream_socket_recvfrom($socket, 1, 0, $peer);
echo "$peer
";
stream_socket_sendto($socket, date("D M j H:i:s Y
"), 0, $peer);
} while ($pkt !== false);
?>
编辑#2:这是另一个有用的PHP 套接字编程教程.它主要是 TCP,但它确实包含有关如何更改代码以改用 UDP 的部分.
Edit #2: Here is another useful tutorial for socket programming in PHP. It is mostly TCP but it does include a section on how to alter the code to use UDP instead.
相关文章