BitSharper.Discovery.DnsDiscovery.GetPeers C# (CSharp) Method

GetPeers() public method

public GetPeers ( ) : IEnumerable
return IEnumerable
        public IEnumerable<EndPoint> GetPeers()
        {
            ICollection<EndPoint> addresses = new HashSet<EndPoint>();

            /*
             * Keep track of how many lookups failed vs. succeeded.
             * We'll throw an exception only if all the lookups fail.
             * We don't want to throw an exception if only one of many lookups fails.
             */
            var failedLookups = 0;

            foreach (var hostName in _hostNames)
            {
                try
                {
                    var hostAddresses = Dns.GetHostEntry(hostName).AddressList;

                    foreach (var inetAddress in hostAddresses)
                    {
                        // DNS isn't going to provide us with the port.
                        // Grab the port from the specified NetworkParameters.
                        var socketAddress = new IPEndPoint(inetAddress, _netParams.Port);

                        // Only add the new address if it's not already in the combined list.
                        if (!addresses.Contains(socketAddress))
                        {
                            addresses.Add(socketAddress);
                        }
                    }
                }
                catch (Exception e)
                {
                    failedLookups++;
                    _log.InfoFormat("DNS lookup for {0} failed.", hostName);

                    if (failedLookups == _hostNames.Length)
                    {
                        // All the lookups failed.
                        // Throw the discovery exception and include the last inner exception.
                        throw new PeerDiscoveryException("DNS resolution for all hosts failed.", e);
                    }
                }
            }
            return addresses;
        }

Usage Example

 /// <exception cref="PeerDiscoveryException"/>
 private static void PrintDns()
 {
     var start = Environment.TickCount;
     var dns = new DnsDiscovery(NetworkParameters.ProdNet());
     PrintAddresses(dns.GetPeers());
     PrintElapsed(start);
 }