Introduced
Silverlight 4.0 support for UDP protocols:
* Udpanysourcemulticastclient-A client that sends information to a multicast group and receives multicast information from any source, that is, the ASM client
Online Demo
Http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
Example
Demonstrates how to implement ASM through Udpanysourcemulticastclient
1. Service End
Form1.cs
Code
// 启动多播的安全策略服务
private void LaunchMulticastPolicyServer()
{
/*
* Silverlight 程序加入多播组之前,会通过 UDP 端口 9430 发送初始消息到多播组,然后服务端返回相应的安全策略(详见文档)
* 为了方便下发安全策略,可以引用 Microsoft.Silverlight.PolicyServers.dll 程序集,其封装了相关的方法
*/
// 本例的安全策略配置结果为:授权通过 UDP 端口 3003 问多播组 224.0.0.1,授权通过 UDP 端口 3006 问多播组 224.0.0.1
MulticastPolicyConfiguration config = new MulticastPolicyConfiguration();
config.AnySourceConfiguration.Add("*", new MulticastResource(IPAddress.Parse("224.0.0.1"), 3006)); // 配置 ASM 的安全策略
MulticastPolicyServer server = new MulticastPolicyServer(config);
server.Start();
ShowMessage("多播的安全策略服务已启动");
}
2. Client
UdpPacketEventArgs.cs
Code
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;
namespace Silverlight40.Communication
{
public class UdpPacketEventArgs : EventArgs
{
// UDP 包的内容
public string Message { get; set; }
// UDP 包的来源的 IPEndPoint
public IPEndPoint Source { get; set; }
public UdpPacketEventArgs(byte[] data, IPEndPoint source)
{
this.Message = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
this.Source = source;
}
}
}