PHP Multicast Multicast Implementation detailed

Source: Internet
Author: User

What is multicast?
网络中存在3中传播形式,单播,广播,多播。 1. 单播 : 就是1->1 2. 广播 : 1->多(广播域内) 3. 多播 : 1->组(一组ip)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5
Relies on IP protocol implementations
首先我们抛出一个问题,要是要你自己在网络中实现这种机制,你会怎么实现呢。哈哈,我的设计大概会是这样。设计思路 : 因为网络是通过ip协议来通信的,那我自然想到是否可以通过某一类标记来标志这种形式就是多播呢,答案肯定是可以的,其实别人也是这样实现的。下面看一张图。
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

D类IP地址(224.0.0.0到239.255.255.255)标记为多播地址。是吧,别人也这样实现啦。下面贴出php代码来给大家证明一把。
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3
multicast_client.php file
    $len =80;$flags =0;$from ="";$port =14446;$group _params =Array"Group" ="228.5.6.7","Interface" = "eth0");  $socket = Socket_create (Af_inet, Sock_dgram, SOL_UDP); Socket_bind ( $socket,  ' 0.0.0.0 ',  $port); socket_set_ Option ( $socket, Ipproto_ip, Mcast_join_group,  $group _ params); Socket_recvfrom ( $socket,  $recvStr,  $len,  $flags,  $from,  $port); echo  "Received".  $recvStr.  "from".  $from socket_close ( $socket);      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
multicast_server.php file
    $port =14446;$group _ip =' 228.5.6.7 '; $group _params = array ( "group" = >  $group _ip,  "interface" =  $send _msg =  "Hello, I am multicast server";  $socket = Socket_create (Af_inet, Sock_dgram, SOL_UDP); Socket_set_option ( $socket, Ipproto_ip, Mcast_join_group,  $group _params); Socket_sendto ( $socket,  $send _msg, strlen ( $send _msg), 0,  $group _ip,  $port); Socket_close ( $socket);           
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

PHP Multicast Multicast Implementation detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.