System.Net.Dns.GetHostAddresses C# (CSharp) Метод

GetHostAddresses() публичный статический Метод

public static GetHostAddresses ( string hostNameOrAddress ) : System.Net.IPAddress[]
hostNameOrAddress string
Результат System.Net.IPAddress[]
        public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(null, hostNameOrAddress);
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostNameOrAddress == null)
            {
                throw new ArgumentNullException(nameof(hostNameOrAddress));
            }

            // See if it's an IP Address.
            IPAddress address;
            IPAddress[] addresses;
            if (IPAddress.TryParse(hostNameOrAddress, out address))
            {
                if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
                {
                    throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress)));
                }
                addresses = new IPAddress[] { address };
            }
            else
            {
                // InternalGetHostByName works with IP addresses (and avoids a reverse-lookup), but we need
                // explicit handling in order to do the ArgumentException and guarantee the behavior.
                addresses = InternalGetHostByName(hostNameOrAddress, true).AddressList;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Exit(null, addresses);
            return addresses;
        }

Usage Example

Пример #1
0
        private void EstablishConnect()
        {
            Receive <Connect>(_ =>
            {
                IPAddress ip;
                if (!IPAddress.TryParse(_host, out ip))
                {
                    ip = Dns.GetHostAddresses(_host).First();
                }

                var endPoint = new IPEndPoint(ip, _port);
                _listener.Tell(new Connecting(_host.ToString(), _port));
                Context.System.Tcp().Tell(new Tcp.Connect(endPoint, timeout: _connectionTimeout));
            });

            Receive <Tcp.Connected>(c =>
            {
                _currentConnection = new CurrentConnection(Sender);
                _currentConnection.Socket.Tell(new Tcp.Register(Self, useResumeWriting: false));
                var greatingMessageTimer = Context.System.Scheduler.ScheduleTellOnceCancelable(_connectionTimeout, Self,
                                                                                               ReceiveTimeout.Instance, Self);

                Become(() => WaitingGreating(greatingMessageTimer));
            });
        }