Communication (communication) Anycast ASM (any source multicast)
Introduced
Distinctive Windows Phone 7.5 (SDK 7.1) communication
Implement "Anycast Multicast"-ASM (any source multicast)
Example
Implementing ASM Channel
UdpAnySourceMulticastChannel.cs
* * Implement an ASM channel (i.e., ASM Help Class) for external invocation * * * * * via udpanysourcemulticastclient to implement ASM (any Source multicast), i.e. SOURCE Multicast "* Multicast group is based on IGMP (internet Group Management Protocol), the Internet Groups Management protocol * * Udpanysourcemulticastclient-one hair A client that sends information to a multicast group and receives multicast information from any source, that is, the ASM client * Beginjoingroup (), Endjoingroup ()-asynchronous method to join the multicast group * Beginreceivefromgroup () , Endreceivefromgroup ()-an asynchronous method of receiving information from a multicast group (which can be understood to receive information sent by all members in a multicast group) * Beginsendtogroup (), Endsendtogroup ()-Asynchronous to send information to a multicast group Method (can be understood to send information to all members within a multicast group) * receivebuffersize-buffer size for receiving information * sendbuffersize-buffer size to send information * * Beginsendto (), Endsendto ()-asynchronous method for sending information to the specified target * blocksource ()-Blocks the specified source from receiving information from the source * Unblocksource ()-Cancel
Block specified source * Multicastloopback-whether the information sent is to be transmitted to itself */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 client Private udpanysourcemulticastclient _client;
Buffer for receiving information private byte[] _buffer;
Whether this client has joined the multicast group private bool _isjoined; <summary>///Constructor///</summary>///<param name= "groupaddress" > Multicast Group address </param>///<param name= "Port" > Multicast group ports </param>///<param name= "Maxmessagesi
Ze > The buffer size for receiving information </param>///<remarks>///Note: The maximum length of UDP message (Datagram) is 65535 (including message headers) </remarks> Public Udpanysourcemulticastchannel (IPAddress groupaddress, int port, int maxmessages IZe) {_buffer = new byte[maxmessagesize];
Instantiating the ASM client requires that the specified parameters be: Multicast group address, multicast group port _client = new Udpanysourcemulticastclient (groupaddress, Port);
The event that is triggered after the multicast information is received is eventhandler<udppacketeventargs> Received;
private void onreceived (IPEndPoint source, byte[] Data {var handler = Received;
if (handler!= null) handler (this, new Udppacketeventargs (data, source));
The events that are triggered after joining the multicast group are public event EventHandler Opening;
private void Onopening () {var handler = Opening;
if (handler!= null) handler (this, eventargs.empty);
The events that are triggered after the multicast group is disconnected are public event EventHandler Closing;
private void OnClosing () {var handler = Closing; if (handler!= null) handler (this, eventargs.empty); ///<summary>///Join multicast group///</summary> public void Open ( ) {if (!_isjoined) {_client. Beginjoingroup (Result => {_client).
Endjoingroup (result);
_isjoined = true;
Deployment.Current.Dispatcher.BeginInvoke (() => {
Onopening ();
Receive ();
});
}, NULL); }///<summary>///sends information to a multicast group, which sends information to all members within the multicast group///</summary&
Gt 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>///receives information from a multicast group that receives information sent by all members within the multicast group///</summary > private void Receive () {if (_isjoined) {Arra Y.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); //Turn off ASM channel public void Close () {_isjoined = Fals
E
OnClosing ();
Dispose (); public void Dispose () {if (_client!= null) _client.
Dispose (); }
}
}