#!/usr/bin/php
<?php

   
/*
    *   listener.php
    *   Joins a multicast group and echoes all data it receives from the group to stdout
    *
    *   (C) 2006, Daniel Rozsnyo <daniel@rozsnyo.com>
    *
    */
    


    // ---------------------------------------------------------------------------------------------- CONFIG
    
    // multicast group and udp port
    
$MC_GROUPADDR '239.255.10.3';
    
$MC_INTERFACE '147.229.212.212';    // the interface where the MCAST JOIN is sent
    
$UDP_PORT '1234';

    

    
// ------------------------------------------------------------------------------------------- PRE-CHECK

    // check for SOCKETS extension
    
if (!extension_loaded('sockets')) {
        die(
"Extension SOCKETS is not available!\n");
    }

    
// check for multicast support
    
if (!defined('IP_ADD_MEMBERSHIP')) {
        die(
"Your PHP does not have MULTICAST support!\n");
    }
    


    
// ------------------------------------------------------------------------------------------------ MAIN
    
    // create socket
    
if (!($socket socket_createAF_INETSOCK_DGRAMSOL_UDP ))) {
        die(
"Can not create socket!\n");
    }

    
// reuse addresses
    
if (!socket_set_option$socketSOL_SOCKETSO_REUSEADDR)) {
        die(
"Can not reuse addresses!?\n");
    }
    
    
// destination address
    
if (!socket_bind$socket'0.0.0.0'$UDP_PORT )) {
        die(
"Can not bind the socket to port!\n");
    }
    
    
// send the multicast request
    
if (!socket_set_option$socketIPPROTO_IPIP_ADD_MEMBERSHIP
                          
, array( 'multiaddr' => $MC_GROUPADDR
                                 
'interface' => $MC_INTERFACE
                                 
)
                          )) {
        die(
"Can not join multicast group!\n");
    }
    
    
// read and echo the data forever
    
while(1) echo socket_read$socket8192 );

?>