与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)...
生活随笔
收集整理的這篇文章主要介紹了
与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)...
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:與眾不同 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
演示 ASM
AnySourceMulticast.xaml
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站降权的原因以及恢复的方法 (二)
- 下一篇: Android菜鸟的成长笔记(11)——