在Raspberry上使用PHP7接收多播

时间:2016-07-22 08:16:22

标签: php debian raspberry-pi2 multicast php-7

我通过PHP(v7.0.8-5)发送多播,这看起来很好。 the capurted packed, on second PC without Firewall

我的问题是我无法收到pakets。

代码很好,没有防火墙工作

这是代码。

<?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。2b)上用ssh启动这个脚本。一个用'r'而另一个用'w'。

1 个答案:

答案 0 :(得分:0)

我现在无法自行测试,但将$dest_port中的socket_recvfrom()更改为$port或将$dest_port的值更改为014446然后再试一次。