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

[索引页]
[源码下载]

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

作者:webabcd

介绍
与众不同 windows phone 7.5 (sdk 7.1) 之通信

  • 实现“任意源多播” - ASM(Any Source Multicast)

示例
实现 ASM 信道
UdpAnySourceMulticastChannel.cs

/** 实现一个 ASM 信道(即 ASM 帮助类),供外部调用* * * 通过 UdpAnySourceMulticastClient 实现 ASM(Any Source Multicast),即“任意源多播”* 多播组基于 IGMP(Internet Group Management Protocol),即“Internet组管理协议”* * UdpAnySourceMulticastClient - 一个发送信息到多播组并从任意源接收多播信息的客户端,即 ASM 客户端*     BeginJoinGroup(), EndJoinGroup() - 加入多播组的异步方法*     BeginReceiveFromGroup(), EndReceiveFromGroup() - 从多播组接收信息的异步方法(可以理解为接收多播组内所有成员发送的信息)*     BeginSendToGroup(), EndSendToGroup() - 发送信息到多播组的异步方法(可以理解为发送信息到多播组内的全部成员)*     ReceiveBufferSize - 接收信息的缓冲区大小*     SendBufferSize - 发送信息的缓冲区大小*     *     BeginSendTo(), EndSendTo() - 发送信息到指定目标的异步方法*     BlockSource() - 阻止指定源,以便不再接收该源发来的信息*     UnblockSource() - 取消阻止指定源*     MulticastLoopback - 发出的信息是否需要传给自己* */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;// 接收信息的缓冲区private byte[] _buffer;// 此客户端是否加入了多播组private bool _isJoined;/// <summary>/// 构造函数/// </summary>/// <param name="groupAddress">多播组地址</param>/// <param name="port">多播组的端口</param>/// <param name="maxMessageSize">接收信息的缓冲区大小</param>/// <remarks>/// 注:udp 报文(Datagram)的最大长度为 65535(包括报文头)/// </remarks>public UdpAnySourceMulticastChannel(IPAddress groupAddress, int port, int maxMessageSize){_buffer = new byte[maxMessageSize];// 实例化 ASM 客户端,需要指定的参数为:多播组地址;多播组的端口_client = new UdpAnySourceMulticastClient(groupAddress, port);}// 收到多播信息后触发的事件public event EventHandler<UdpPacketEventArgs> Received;private void OnReceived(IPEndPoint source, byte[] data){var handler = Received;if (handler != null)handler(this, new UdpPacketEventArgs(data, source));}// 加入多播组后触发的事件public event EventHandler Opening;private void OnOpening(){var handler = Opening;if (handler != null)handler(this, EventArgs.Empty);}// 断开多播组后触发的事件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>/// 发送信息到多播组,即发送信息到多播组内的所有成员/// </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>/// 从多播组接收信息,即接收多播组内所有成员发送的信息/// </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);}}// 关闭 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="发送" 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{// 实例化自定义的 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 是代表同一个物理网络中所有系统的多播组地址,而 224.0.0.2 代表同一个物理网络中的所有路由器_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();// 需要的使用,应该调用 Close()// _channel.Close();
        }void _channel_Opening(object sender, EventArgs e){_channel.Send(string.Format("{0}: 进来了 - [{1}]", txtName.Text, DateTime.Now.ToString("HH:mm:ss")));}void _channel_Received(object sender, UdpPacketEventArgs e){// 因为已经指定了接收信息的缓冲区大小是 2048 ,所以如果信息不够 2048 个字节的的话,空白处均为空字节“\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
[源码下载]

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

  1. 与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信

    原文:与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信 [索引页] [源码下载] 与众不同 windows phone (29) - C ...

  2. python socket编程 ws2tcpip_Python3组播通信编程实现教程(发送者+接收者)

    一.说明 1.1 标准组播解释 通信分为单播.多播(即组播).广播三种方式 单播指发送者发送之后,IP数据包被路由器发往目的IP指定的唯一一台设备的通信形式,比如你现在与web服务器通信就是单播形式 ...

  3. windows7 python 指定源组播 10049_Python3组播通信编程实现教程(发送者+接收者)

    一.说明 1.1 标准组播解释 通信分为单播.多播(即组播).广播三种方式 单播指发送者发送之后,IP数据包被路由器发往目的IP指定的唯一一台设备的通信形式,比如你现在与web服务器通信就是单播形式 ...

  4. [转]UDP(udp通信、广播、组播),本地套接字

    转发参考链接: [陈宸-研究僧] https://blog.csdn.net/qq_35883464/article/details/103741461 [Alliswell_WP] https:// ...

  5. c/c++:UDP(udp通信、广播、组播),本地套接字

    目录 1. udp 1.1 udp通信流程 1.2 操作函数 send.sendto recv.recvfrom 2. 广播 2.1 广播通信流程 2.2 设置广播属性函数:setsockopt 2. ...

  6. Qt网络编程之搭建Udp通信【单播、组播、广播】

    由于项目的环境实在局域网内进行传输,所以采用了UDP通信.为此记录一下. UDP概念 UDP(用户数据报协议)是一个简单的面向数据报的传输层协议.提供的是非面向连接的.不可靠的数据流传输.UDP不提供 ...

  7. 与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)...

    原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页] [源码下载] 与众不同 windows phone (2 ...

  8. 与众不同 windows phone (15) - Media(媒体)之后台播放音频

    原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页] [源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音 ...

  9. 与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态

    原文:与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态 [索引页] [源码下载] 与众不同 windows phone (23) - Devi ...

最新文章

  1. 选择PHP,选择自由与开源
  2. 在fedora下面安装ftp服务器
  3. 阿里给所有卖家发福利:全球首个人工智能中文字库免费用
  4. QML工作笔记-使用QML中的Date将时间戳和指定格式时间互转
  5. __property 关键字的使用
  6. php 数据库时间具体到分钟,php – 在设定的到期时间后删除数据库行(例如5分钟)...
  7. 同时安装 Python 2 和 Python 3环境下 pip 的使用
  8. Android开发1、2周——GeoQuiz项目
  9. 神兽传说JAVA下载_神兽传说3-救赎大陆
  10. 如何卸载AutoCAD 2019,彻底卸载MAC版CAD教程
  11. Jaspergold形式验证-vhdl语言
  12. wordpress最佳架构_动物和宠物的24个最佳WordPress主题
  13. 三星为Ativ S发布WP8更新
  14. MRT退休后的HEG(HDF-EOS To GeoTIFF Conversion Tool )工具安装
  15. Glide加载网络图片出现模糊问题
  16. 计算机输入开机密码无法进入,电脑开机无法输入密码怎么办
  17. Fusion360显示模糊怎么办?
  18. Spring-04-Spring的入门配置
  19. AOV网络——初了解
  20. 2023计算机毕业设计SSM最新选题之javaJava青年志愿者信息管理系统15925

热门文章

  1. 改变 PropertyGrid 控件的编辑风格(2)——编辑多行文本
  2. Log4j每天、每小时、每分钟定时生成日志文件
  3. hdu4585 STL水题
  4. 操作系统原理第二章:操作系统结构
  5. 【Linux 内核 内存管理】Linux 内核内存布局 ③ ( Linux 内核 动态分配内存 系统接口函数 | 统计输出 vmalloc 分配的内存 )
  6. 【Android 启动过程】Activity 启动源码分析 ( AMS -> ActivityThread、AMS 线程阶段 )
  7. 【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 使用文档 | 创建 Protobuf 源文件 | Protobuf 语法 )
  8. 【Android 安装包优化】WebP 应用 ( 4.0 以下兼容 WebP | Android Studio 中使用 libwebp.so 库向下兼容版本 | libwebp 库测试可用性 )
  9. 【Flutter】StatefulWidget 组件 ( PageView 组件 )
  10. 会声会影x7 每次安装均会提示:已安装这个产品的另一个版本