/// <summary> /// Handle the async DNS response. /// </summary> /// <param name="ar"></param> private void OnResolved(IAsyncResult ar) { try { IPHostEntry ent = Dns.EndGetHostEntry(ar); if (ent.AddressList.Length <= 0) { m_ip = null; } else { // From docs: // When hostName is a DNS-style host name associated with multiple IP addresses, // only the first IP address that resolves to that host name is returned. m_ip = ent.AddressList[0]; } } catch (Exception e) { Debug.WriteLine(e.ToString()); m_ip = null; } AddressResolved callback = (AddressResolved)ar.AsyncState; if (callback != null) { callback(this); } }
/// <summary> /// Async DNS lookup. IP will be null in callback on failure. Callback will /// be called immediately if IP is already known (e.g. dotted-quad). /// </summary> /// <param name="callback">Called when resolution complete.</param> public void Resolve(AddressResolved callback) { if ((m_ip != null) && (m_ip != IPAddress.Any) && (m_ip != IPAddress.IPv6Any) ) { callback(this); } else { Dns.BeginGetHostEntry(m_hostname, new AsyncCallback(OnResolved), callback); } }
/// <summary> /// Async DNS lookup. IP will be null in callback on failure. Callback will /// be called immediately if IP is already known (e.g. dotted-quad). /// </summary> /// <param name="callback">Called when resolution complete.</param> public void Resolve(AddressResolved callback) { if ((m_ip != null) && (m_ip != IPAddress.Any) && (m_ip != IPAddress.IPv6Any) ) { callback(this); } else Dns.BeginGetHostEntry(m_hostname, new AsyncCallback(OnResolved), callback); }