如何使用 PHP 通过 UDP 将数据发送到 IP 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/687765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I send data with PHP to an IP address via UDP?
提问by TheTXI
How can I send data with PHP to an IP address via UDP?
如何使用 PHP 通过 UDP 将数据发送到 IP 地址?
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 />\n";
}
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;
?>
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?
此代码将“kik”与我可以在另一台计算机上读取的程序一起发送,但是如何在浏览器中看到它?
回答by TheTXI
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.
我的 PHP 知识有点生疏,所以我一直在做一些搜索,试图找到一些好的指南和教程。这个PHP Sockets Made Easy看起来对你来说是一个很好的入门指南。
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 Manualhas some more information specifically regarding UDP:
编辑:我发布的原始文章没有详细介绍 UDP,所以我删除了以前的代码。PHP手册中的文章有一些关于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\n";
stream_socket_sendto($socket, date("D M j H:i:s Y\r\n"), 0, $peer);
} while ($pkt !== false);
?>
Edit #2: Here is another useful tutorialfor socket programming in PHP. It is mostly TCP but it does include a section on how to alter the code to use UDP instead.
编辑 #2:这是另一个有用的 PHP 套接字编程教程。它主要是 TCP,但它确实包含一个关于如何更改代码以使用 UDP 的部分。
回答by Eddy
Just pulled this snippet out of some working code I have
刚刚从我拥有的一些工作代码中提取了这个片段
if (!socket_bind($sh, LISTENIP, LISTENPORT)) exit("Could not bind to socket");
while (TRUE) {
// $z = socket_recvfrom($sh, $data, 65535, 0, $connectip, $connectPort);
while(socket_recvfrom($sh, $data, 65535, 0, $connectip, $connectPort)) {
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else { #START ELSE COULD FORK
$PIDS[$pid] = $pid; //KEEP TRACK OF SPAWNED PIDS
if ($pid) {
//PARENT THREAD : $ch is a copy that we don't need in this thread
} else {
/** CHILD THREAD::BEGIN PROCESSING THE CONNECTION HERE! **/
include "include/child_thread.inc.php";
} //Child Thread
}//if-else-forked
/** CLEANUP THE CHILD PIDs HERE :: "Any system resources used by the child are freed." **/
foreach ($PIDS as $pid) pcntl_waitpid($pid,$status,WNOHANG);
$i++; //INCREASE CONNECTION COUNTER
}//While socket_accept
/** CLEANUP THE PARENT PIDS **/
foreach ($PIDS as $pid) {
$returnPid = pcntl_waitpid($pid,$status);
unset($PIDS[$pid]);
}
}//While True
回答by Eddy
<?php
$server_ip = '127.0.0.1';
$server_port = 43278;
$beat_period = 5;
$message = 'PyHB';
print "Sending heartbeat to IP $server_ip, port $server_port\n";
print "press Ctrl-C to stop\n";
if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
while (1) {
socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);
print "Time: " . date("%r") . "\n";
sleep($beat_period);
}
} else {
print("can't create socket\n");
}
?>
回答by Henrik Paul
I think you'll find that the PHP's socket referenceis a good place to study on this topic.
我想您会发现PHP 的套接字参考是研究该主题的好地方。

