System.Net.NameResolutionPal.GetHostByName C# (CSharp) Method

GetHostByName() public static method

public static GetHostByName ( string hostName ) : IPHostEntry
hostName string
return IPHostEntry
        public static IPHostEntry GetHostByName(string hostName)
        {
            //
            // IPv6 disabled: use gethostbyname() to obtain DNS information.
            //
            IntPtr nativePointer =
                Interop.Winsock.gethostbyname(
                    hostName);

            if (nativePointer == IntPtr.Zero)
            {
                // Need to do this first since if we wait the last error code might be overwritten.
                SocketException socketException = new SocketException();

                IPAddress address;
                if (IPAddress.TryParse(hostName, out address))
                {
                    IPHostEntry ipHostEntry = NameResolutionUtilities.GetUnresolvedAnswer(address);
                    if (NetEventSource.IsEnabled) NetEventSource.Exit(null, ipHostEntry);
                    return ipHostEntry;
                }

                throw socketException;
            }

            return NativeToHostEntry(nativePointer);
        }

Usage Example

Example #1
0
        private static IPHostEntry InternalGetHostByName(string hostName, bool includeIPv6)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostByName", hostName);
            }
            IPHostEntry ipHostEntry = null;

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("Dns.GetHostByName: " + hostName);
            }

            if (hostName.Length > MaxHostName || // If 255 chars, the last one must be a dot.
                hostName.Length == MaxHostName && hostName[MaxHostName - 1] != '.')
            {
                throw new ArgumentOutOfRangeException(nameof(hostName), SR.Format(SR.net_toolong,
                                                                                  nameof(hostName), MaxHostName.ToString(NumberFormatInfo.CurrentInfo)));
            }

            //
            // IPv6 Changes: IPv6 requires the use of getaddrinfo() rather
            //               than the traditional IPv4 gethostbyaddr() / gethostbyname().
            //               getaddrinfo() is also protocol independent in that it will also
            //               resolve IPv4 names / addresses. As a result, it is the preferred
            //               resolution mechanism on platforms that support it (Windows 5.1+).
            //               If getaddrinfo() is unsupported, IPv6 resolution does not work.
            //
            // Consider    : If IPv6 is disabled, we could detect IPv6 addresses
            //               and throw an unsupported platform exception.
            //
            // Note        : Whilst getaddrinfo is available on WinXP+, we only
            //               use it if IPv6 is enabled (platform is part of that
            //               decision). This is done to minimize the number of
            //               possible tests that are needed.
            //
            if (includeIPv6 || SocketProtocolSupportPal.OSSupportsIPv6)
            {
                //
                // IPv6 enabled: use getaddrinfo() to obtain DNS information.
                //
                int         nativeErrorCode;
                SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, out ipHostEntry, out nativeErrorCode);
                if (errorCode != SocketError.Success)
                {
                    throw SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode);
                }
            }
            else
            {
                ipHostEntry = NameResolutionPal.GetHostByName(hostName);
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostByName", ipHostEntry);
            }
            return(ipHostEntry);
        } // GetHostByName
All Usage Examples Of System.Net.NameResolutionPal::GetHostByName