Communication(通訊)之源特定組播 SSM(Source Specific Multicast)
樣本
1、服務端
Main.cs
/* * 此服務會定時向指定的多播組發送訊息,用於示範 SSM */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace SocketServerSSM { public partial class Main : Form { System.Threading.SynchronizationContext _syncContext; public Main() { InitializeComponent(); LaunchSocketUdp(); } private void LaunchSocketUdp() { _syncContext = System.Threading.SynchronizationContext.Current; // 定義 Source Specific Multicast 中的 Source,即 SSM 用戶端僅接收此 Source 發送到多播組的資料 IPEndPoint sourcePoint = new IPEndPoint(IPAddress.Any, 3370); // 定義多播組 IPEndPoint multicastPoint = new IPEndPoint(IPAddress.Parse("224.0.1.2"), 3369); UdpClient sourceUdp = new UdpClient(sourcePoint); ShowMessage("用於示範 SSM 的 Socket 服務已啟動,每 3 秒向多播組發送一次資訊"); // 每 3 秒向多播組發送一次資訊 var timer = new System.Timers.Timer(); timer.Interval = 3000d; timer.Elapsed += delegate { string msg = string.Format("{0} - {1}", Dns.GetHostName(), DateTime.Now.ToString("HH:mm:ss")); byte[] data = Encoding.UTF8.GetBytes(msg); sourceUdp.Send(data, data.Length, multicastPoint); }; timer.Start(); } public void ShowMessage(string msg) { txtMsg.Text += msg + "\r\n"; } } }