Slow and steady Silverlight (55)

Source: Internet
Author: User
Tags config socket thread tostring silverlight

Introduced

Silverlight 4.0 support for UDP protocols:

* Udpsinglesourcemulticastclient-a client that receives multicast information from a single source, the SSM client

Online Demo

Http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html

Example

Demonstrates how to implement the SSM through Udpsinglesourcemulticastclient

1. Service End

Form1.cs

Code

Start the Socket service (based on UDP protocol) for demonstrating the SSM
private void Launchsocketudp ()
{
Service-Side IPEndPoint
IPEndPoint serverpoint = new IPEndPoint (Ipaddress.any, 3004);
IPEndPoint of broadcast address (255.255.255.255)
IPEndPoint broadcastpoint = new IPEndPoint (Ipaddress.broadcast, 3003);

Create a server-side UDP object based on the ipendpoint of the service side
UdpClient serverudp = new UdpClient (serverpoint);
ShowMessage ("The Socket Service for the demo SSM has started (based on UDP Protocol)");

Broadcast once per 3 table
var timer = new System.Timers.Timer ();
Timer. Interval = 3000d;
Timer. Elapsed + + Delegate
{
String msg = string. Format ("{0}: {1}-[{2}]", Dns.gethostname (), "Broadcast content", DateTime.Now.ToString ("HH:mm:ss"));
byte[] data = Encoding.UTF8.GetBytes (msg);

Send broadcast (specify separately: broadcast content; bytes of broadcast content; IPEndPoint of broadcast address)
Serverudp.send (data, data.) Length, Broadcastpoint);
};
Timer. Start ();

Receive information sent by client
var thread = new System.Threading.Thread (() =>
{
while (true)
{
IPEndPoint clientpoint = null;
Receives the information from the client and obtains the client's IPEndPoint
byte[] Receivedbytes = serverudp.receive (ref clientpoint);
String receiveddata = Encoding.UTF8.GetString (receivedbytes);
String message = String. Format ("{0}-from: {1}", Receiveddata, Clientpoint.tostring ());

_synccontext.post (Delegate {showmessage (message);}, NULL);
}
});
Thread. Start ();
}

To start the Multicast Security Policy Service
private void Launchmulticastpolicyserver ()
{
/*
* Before the Silverlight program joins a multicast group, the initial message is sent to the multicast group via UDP Port 9430, and then the server returns the appropriate security policy (see documentation)
* To facilitate the issuance of security policy, you can refer to the Microsoft.Silverlight.PolicyServers.dll assembly, which encapsulates the relevant methods
*/

The security policy configuration result of this example is: Authorization via UDP port 3003 to ask multicast group 224.0.0.1, authorization through UDP port 3006 ask multicast group 224.0.0.1
multicastpolicyconfiguration config = new multicastpolicyconfiguration ();
Config. Singlesourceconfiguration.add ("*", New Multicastresource (Ipaddress.parse ("224.0.0.1"), 3003); Configuring the SSM Security Policy

Multicastpolicyserver Server = new Multicastpolicyserver (config);
Server. Start ();

ShowMessage ("Multicast security Policy Service started");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.