我通过PHP (v7.0.8-5)发送多播,到目前为止,这看起来还不错。

我的问题是我不能接收到这些小伙伴。
代码很好,无需防火墙就可以工作。
这是密码。
<?php
error_reporting(E_ALL | E_STRICT);
$mesg = "123456789012" ; //the msg i want to send
$ip = "228.5.6.7"; //the ip to send
$port = 14446 ; //the port to send
//build the socket
$grpparms = array("group"=>$ip,"interface"=>"eth0") ;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($sock,IPPROTO_IP,MCAST_JOIN_GROUP,$grpparms);
if($argv[1] == 'r'){ //read multicasts
$from = '';
$dest_port = 0;
$binded = socket_bind($sock, '0.0.0.0',$port);
socket_recvfrom($sock, $buf, 12, 0, $from, $dest_port);
echo "Received $buf from remote address $from and remote port $dest_port" . PHP_EOL;
}
if ($argv[1] =='w'){ //write multicasts
socket_sendto($sock, $mesg, strlen($mesg), 0, $ip,$port);
echo "Send '$mesg' to $ip at port $port".PHP_EOL;
socket_close($sock) ;
}
?>我称它为参数“r”来读/接收,用“w”来发送/写多个广播。
我在两个不同的raspberrys (Vers )上通过ssh启动这个脚本。2b)在同一开关上。一个是'r‘,另一个是'w’。
发布于 2016-07-22 13:12:31
我现在无法亲自测试它,但是请将socket_recvfrom()中的socket_recvfrom()更改为$port,或者将$dest_port的值从0更改为14446,然后再试一次。
https://stackoverflow.com/questions/38521482
复制相似问题