internal static void Join() { // Initialize the receive buffer _receiveBuffer = new byte[MAX_MESSAGE_SIZE]; // Create the UdpAnySourceMulticastClient instance using the defined // GROUP_ADDRESS and GROUP_PORT constants. UdpAnySourceMulticastClient is a // client receiver for multicast traffic from any source, also known as Any Source Multicast (ASM) _client = new UdpAnySourceMulticastClient(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT); // Make a request to join the group. _client.BeginJoinGroup( result => { // Complete the join _client.EndJoinGroup(result); // The MulticastLoopback property controls whether you receive multicast // packets that you send to the multicast group. Default value is true, // meaning that you also receive the packets you send to the multicast group. // To stop receiving these packets, you can set the property following to false _client.MulticastLoopback = true; // Set a flag indicating that we have now joined the multicast group _joined = true; Receive(); }, null); }
public void Find(Action<IPAddress> callback) { FoundCallback = callback; MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber); MulticastSocket.BeginJoinGroup((result) => { try { MulticastSocket.EndJoinGroup(result); GroupJoined(result); } catch (Exception ex) { Debug.WriteLine("EndjoinGroup exception {0}", ex.Message); // This can happen eg when wifi is off FoundCallback(null); } }, null); }
public IPAddress Find() { WaitEvent.Reset(); MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber); MulticastSocket.BeginJoinGroup((result) => { try { MulticastSocket.EndJoinGroup(result); GroupJoined(result); } catch (Exception ex) { WaitEvent.Set(); Debug.WriteLine("EndjoinGroup exception {0}", ex.Message); } }, null); WaitEvent.WaitOne(); return FoundIPAddress; }
public void Open(Action<bool> callback) { try { IsOpen = false; channel = new UdpAnySourceMulticastClient(IPAddress.Parse(Address), Port); _openResult = channel.BeginJoinGroup((result) => { channel.EndJoinGroup(result); IsOpen = true; callback(true); }, null); } catch { callback(false); } }
private static UdpAnySourceMulticastClient CreateMulticastClientAndJoinGroup(string ipAddress, int localPort) { var retVal = new UdpAnySourceMulticastClient(IPAddress.Parse(ipAddress), localPort); var signal = new System.Threading.ManualResetEvent(false); try { retVal.BeginJoinGroup( (joinResult) => { retVal.EndJoinGroup(joinResult); retVal.MulticastLoopback = true; retVal.SendBufferSize = retVal.ReceiveBufferSize = SsdpConstants.DefaultUdpSocketBufferSize; signal.Set(); }, null); signal.WaitOne(); } finally { signal.Dispose(); } return retVal; }