日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)...

發(fā)布時(shí)間:2025/6/15 windows 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:與眾不同 windows phone (32) - Communication(通信)之任意源組播 ASM(Any Source Multicast)

[索引頁]
[源碼下載]


與眾不同 windows phone (32) - Communication(通信)之任意源組播?ASM(Any Source Multicast)



作者:webabcd


介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之通信

  • 實(shí)現(xiàn)“任意源多播” - ASM(Any Source Multicast)



示例
實(shí)現(xiàn) ASM 信道
UdpAnySourceMulticastChannel.cs

/** 實(shí)現(xiàn)一個(gè) ASM 信道(即 ASM 幫助類),供外部調(diào)用* * * 通過 UdpAnySourceMulticastClient 實(shí)現(xiàn) ASM(Any Source Multicast),即“任意源多播”* 多播組基于 IGMP(Internet Group Management Protocol),即“Internet組管理協(xié)議”* * UdpAnySourceMulticastClient - 一個(gè)發(fā)送信息到多播組并從任意源接收多播信息的客戶端,即 ASM 客戶端* BeginJoinGroup(), EndJoinGroup() - 加入多播組的異步方法* BeginReceiveFromGroup(), EndReceiveFromGroup() - 從多播組接收信息的異步方法(可以理解為接收多播組內(nèi)所有成員發(fā)送的信息)* BeginSendToGroup(), EndSendToGroup() - 發(fā)送信息到多播組的異步方法(可以理解為發(fā)送信息到多播組內(nèi)的全部成員)* ReceiveBufferSize - 接收信息的緩沖區(qū)大小* SendBufferSize - 發(fā)送信息的緩沖區(qū)大小* * BeginSendTo(), EndSendTo() - 發(fā)送信息到指定目標(biāo)的異步方法* BlockSource() - 阻止指定源,以便不再接收該源發(fā)來的信息* UnblockSource() - 取消阻止指定源* MulticastLoopback - 發(fā)出的信息是否需要傳給自己* */using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes;using System.Text; using System.Net.Sockets;namespace Demo.Communication.SocketClient {public class UdpAnySourceMulticastChannel : IDisposable{// ASM 客戶端private UdpAnySourceMulticastClient _client;// 接收信息的緩沖區(qū)private byte[] _buffer;// 此客戶端是否加入了多播組private bool _isJoined;/// <summary>/// 構(gòu)造函數(shù)/// </summary>/// <param name="groupAddress">多播組地址</param>/// <param name="port">多播組的端口</param>/// <param name="maxMessageSize">接收信息的緩沖區(qū)大小</param>/// <remarks>/// 注:udp 報(bào)文(Datagram)的最大長度為 65535(包括報(bào)文頭)/// </remarks>public UdpAnySourceMulticastChannel(IPAddress groupAddress, int port, int maxMessageSize){_buffer = new byte[maxMessageSize];// 實(shí)例化 ASM 客戶端,需要指定的參數(shù)為:多播組地址;多播組的端口_client = new UdpAnySourceMulticastClient(groupAddress, port);}// 收到多播信息后觸發(fā)的事件public event EventHandler<UdpPacketEventArgs> Received;private void OnReceived(IPEndPoint source, byte[] data){var handler = Received;if (handler != null)handler(this, new UdpPacketEventArgs(data, source));}// 加入多播組后觸發(fā)的事件public event EventHandler Opening;private void OnOpening(){var handler = Opening;if (handler != null)handler(this, EventArgs.Empty);}// 斷開多播組后觸發(fā)的事件public event EventHandler Closing;private void OnClosing(){var handler = Closing;if (handler != null)handler(this, EventArgs.Empty);}/// <summary>/// 加入多播組/// </summary>public void Open(){if (!_isJoined){_client.BeginJoinGroup(result =>{_client.EndJoinGroup(result);_isJoined = true;Deployment.Current.Dispatcher.BeginInvoke(() =>{OnOpening();Receive();});}, null);}}/// <summary>/// 發(fā)送信息到多播組,即發(fā)送信息到多播組內(nèi)的所有成員/// </summary>public void Send(string msg){if (_isJoined){byte[] data = Encoding.UTF8.GetBytes(msg);_client.BeginSendToGroup(data, 0, data.Length,result =>{_client.EndSendToGroup(result);}, null);}}/// <summary>/// 從多播組接收信息,即接收多播組內(nèi)所有成員發(fā)送的信息/// </summary>private void Receive(){if (_isJoined){Array.Clear(_buffer, 0, _buffer.Length);_client.BeginReceiveFromGroup(_buffer, 0, _buffer.Length,result =>{IPEndPoint source;_client.EndReceiveFromGroup(result, out source);Deployment.Current.Dispatcher.BeginInvoke(() =>{OnReceived(source, _buffer);Receive();});}, null);}}// 關(guān)閉 ASM 信道public void Close(){_isJoined = false;OnClosing();Dispose();}public void Dispose(){if (_client != null)_client.Dispose();}} }


演示 ASM
AnySourceMulticast.xaml

<phone:PhoneApplicationPage x:Class="Demo.Communication.SocketClient.AnySourceMulticast"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"FontFamily="{StaticResource PhoneFontFamilyNormal}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"shell:SystemTray.IsVisible="True"><Grid x:Name="LayoutRoot" Background="Transparent"><StackPanel HorizontalAlignment="Left"><ListBox Name="lstAllMsg" MaxHeight="400" /><TextBox x:Name="txtName" /><TextBox x:Name="txtInput" KeyDown="txtInput_KeyDown" /><Button x:Name="btnSend" Content="發(fā)送" Click="btnSend_Click" /></StackPanel> </Grid></phone:PhoneApplicationPage>

AnySourceMulticast.xaml.cs

/** 用于演示 ASM*/using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls;using System.Windows.Navigation;namespace Demo.Communication.SocketClient {public partial class AnySourceMulticast : PhoneApplicationPage{// 實(shí)例化自定義的 ASM 信道private UdpAnySourceMulticastChannel _channel;public AnySourceMulticast(){InitializeComponent();}protected override void OnNavigatedTo(NavigationEventArgs e){txtName.Text = "匿名" + new Random().Next(1000, 9999).ToString();// 多播組地址是必須介于 224.0.0.0 到 239.255.255.255 之間的 IP 地址,其中范圍介于 224.0.0.0 到 224.0.0.255 之間的多播地址是保留多播地址// 比如:224.0.0.0 是基址,224.0.0.1 是代表同一個(gè)物理網(wǎng)絡(luò)中所有系統(tǒng)的多播組地址,而 224.0.0.2 代表同一個(gè)物理網(wǎng)絡(luò)中的所有路由器_channel = new UdpAnySourceMulticastChannel(IPAddress.Parse("224.0.1.1"), 3368, 2048); _channel.Opening += new EventHandler(_channel_Opening);_channel.Received += new EventHandler<UdpPacketEventArgs>(_channel_Received);_channel.Closing += new EventHandler(_channel_Closing);_channel.Open();// 需要的使用,應(yīng)該調(diào)用 Close()// _channel.Close(); }void _channel_Opening(object sender, EventArgs e){_channel.Send(string.Format("{0}: 進(jìn)來了 - [{1}]", txtName.Text, DateTime.Now.ToString("HH:mm:ss")));}void _channel_Received(object sender, UdpPacketEventArgs e){// 因?yàn)橐呀?jīng)指定了接收信息的緩沖區(qū)大小是 2048 ,所以如果信息不夠 2048 個(gè)字節(jié)的的話,空白處均為空字節(jié)“\0”string message = string.Format("{0} - 來自:{1}", e.Message.TrimEnd('\0'), e.Source.ToString()); lstAllMsg.Items.Insert(0, message); }void _channel_Closing(object sender, EventArgs e){_channel.Send(string.Format("{0}: 離開了 - [{1}]", txtName.Text, DateTime.Now.ToString("HH:mm:ss")));}private void btnSend_Click(object sender, RoutedEventArgs e){SendMsg();}private void txtInput_KeyDown(object sender, KeyEventArgs e){if (e.Key == Key.Enter){SendMsg();this.Focus();}}private void SendMsg(){_channel.Send(string.Format("{0}: {1} - [{2}]", txtName.Text, txtInput.Text, DateTime.Now.ToString("HH:mm:ss")));txtInput.Text = "";}} }



OK
[源碼下載]

總結(jié)

以上是生活随笔為你收集整理的与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。