PHP socket_recv不检索多播数据

时间:2017-12-10 18:18:44

标签: php windows multicast

我正在尝试阅读SMA EMeter在Windows Server 2016(PHP版本7.1)下发送的多播数据

    // Listen to Multicast
    $port = 9522 ;  // Multicast Port used by SMA
    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    echo "Read SMA Energymeter ->  socket creation done: ".$socket."\n"; 
    $bindip="0.0.0.0"; // local unspecified
    if(!socket_bind($socket,$bindip,$port)) {echo "Read SMA Energymeter ->  bind failed\n"; exit();  }
    echo "Read SMA Energymeter ->  bind on port: ".$port." ip: ".$bindip." done\n";

    $adress = "239.12.255.254"; // Multicast IP used by SMA
    $ret=socket_set_option($socket,IPPROTO_IP,MCAST_JOIN_GROUP,array("group"=>$adress,"interface"=>0));
    if ($ret === false) {echo "Read SMA Energymeter -> Unable to join multicast group";exit();  }
    echo "Read SMA Energymeter ->  MCAST join sucessfully done: ".$adress."\n";   

    $fromport= 0;
    $flags   = 0 ;
    $from = '';//"192.168.1.116" ;
    $raw = "That is the return buffer";
    $len = 700 ; // sind rund 600 Bytes vom SMA zu erwarten

    echo "Read SMA Energymeter ->  start retrieval. Len:".$len."\n";        
    $bytes=socket_recvfrom($socket,$raw,$len,$flags,$from,$fromport);
    //$bytes=socket_recv($socket,$raw,$len,$flags);
    echo "Read SMA Energymeter ->  socket recieved done. Bytes:".$bytes."\n";

问题socket_recv永远不会返回。看起来没有检索到数据。 Wireshark每秒钟显示1-2个数据包(每个600Bytes)[1]。 Perf监视器显示php正在侦听端口9522 [2]。

https://i.stack.imgur.com/jjYYa.png [1]

https://i.stack.imgur.com/4uQqt.png [2]

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:0)

问题是在Windows下,传出的MC IF基于路由表。 如果使用多个NIC,则需要删除MC路由并将其添加到要使用的IF上。就像我的情况一样:

route delete 224.0.0.0 mask 240.0.0.0
route add 224.0.0.0 mask 240.0.0.0 192.168.1.63

显然,其他可能需要在之前发送MCAST的应用程序将不再起作用:(