php multicast多播實現詳解

來源:互聯網
上載者:User

標籤:圖片   5.6   tty   cvs   答案   haskell   ntp   存在   標記   

什麼是多播?
網路中存在3中傳播形式,單播,廣播,多播。 1. 單播 : 就是1->1 2. 廣播 : 1->多(廣播域內) 3. 多播 : 1->組(一組ip)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
依賴於ip協議實現
首先我們拋出一個問題,要是要你自己在網路中實現這種機制,你會怎麼實現呢。哈哈,我的設計大概會是這樣。設計思路 : 因為網路是通過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檔案
    $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檔案
    $port = 14446;    $group_ip = ‘228.5.6.7‘;    $group_params = array(        "group" => $group_ip,        "interface" => "eth0"        );    $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多播實現詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.