简单解决SL多播授权
SL特定源多播和任意源多播,加入组前,需要获得当前域的9430端口的授权。
请求授权的消息叫“公告”,响应的消息叫“授权”。分别用0和1表示。
公告和身份验证消息的格式如下所示: struct SilverlightPolicyMessage { byte[3] header = "SL\0"; byte version = 1; byte messageType = {Announcement: 0, Authorization: 1}; byte[4] messageId; byte[2] port; byte[2] applicationOriginUriLength; byte groupAddressLength; byte[applicationOriginUriLength] applicationOriginUri; byte[groupAddressLength] groupAddress; }
如果确认应用程序源URI有权加入申请的多播组,那么把消息类型的第5个字节修改为1,表示通过授权,作为响应数据即可:
以下代码测试通过:
static UdpClient client = null; static void Main(string[] args) { client = new UdpClient(9430);//SL客户端客户端请求授权时,会向9430端口 验证授权 client.BeginReceive(ReceiveCallback, null); Console.Read(); } static void ReceiveCallback(IAsyncResult result) { IPEndPoint remoteEP = null; byte[] Announcement = client.EndReceive(result, ref remoteEP);//接收到公告 if (result.IsCompleted) { Console.WriteLine(remoteEP.Address.ToString() + ":" + remoteEP.Port); Announcement[4] = 1;//第5个字节0表示公告,1表示授权。其他数据不变,将消息改为授权作为响应 client.BeginSend(Announcement, Announcement.Length,remoteEP, SendCallback, null); Console.WriteLine("Done"); } } static void SendCallback(IAsyncResult Asyncresult) { client.EndSend(Asyncresult); if (Asyncresult.IsCompleted) { Console.WriteLine("Sended"); } }
这样处理后,可以使多播应用程序的调试变得更简单。