예제 #1
0
		/// <summary>
		/// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname (asynchronous version)
		/// </summary>
		public static void ResolveAsync(string ipOrHost, ResolveAddressCallback callback)
		{
			if (string.IsNullOrEmpty(ipOrHost))
				throw new ArgumentException("Supplied string must not be empty", "ipOrHost");

			ipOrHost = ipOrHost.Trim();

			IPAddress ipAddress = null;
			if (IPAddress.TryParse(ipOrHost, out ipAddress))
			{
				if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
				{
					callback(ipAddress);
					return;
				}
				throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
			}

			// ok must be a host name
			IPHostEntry entry;
			try
			{
				Dns.BeginGetHostEntry(ipOrHost, delegate(IAsyncResult result)
				{
					entry = Dns.EndGetHostEntry(result);

					if (entry == null)
					{
						callback(null);
						return;
					}

					// check each entry for a valid IP address
					foreach (IPAddress ipCurrent in entry.AddressList)
					{
						if (ipCurrent.AddressFamily == AddressFamily.InterNetwork)
						{
							callback(ipCurrent);
							return;
						}
					}

					callback(null);
				}, null);
			}
			catch (SocketException ex)
			{
				if (ex.SocketErrorCode == SocketError.HostNotFound)
				{
					//LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
					callback(null);
				}
				else
				{
					throw;
				}
			}
		}
예제 #2
0
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            registerServiceCallback = new RegisterServiceCallback(OnRegisterService);
            addPeerCallback         = new AddPeerCallback(OnAddPeer);
            removePeerCallback      = new RemovePeerCallback(OnRemovePeer);
            resolveServiceCallback  = new ResolveServiceCallback(OnResolveService);
            resolveAddressCallback  = new ResolveAddressCallback(OnResolveAddress);
            readMessageCallback     = new ReadMessageCallback(OnReadMessage);

            this.Load += new System.EventHandler(this.Form1_Load);

            this.AcceptButton = button1;
        }
        /// <summary>
        /// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname (asynchronous version)
        /// </summary>
        public static void ResolveAsync(string ipOrHost, ResolveAddressCallback callback)
        {
            if (string.IsNullOrEmpty(ipOrHost))
            {
                throw new ArgumentException("Supplied string must not be empty", "ipOrHost");
            }

            ipOrHost = ipOrHost.Trim();

            NetAddress ipAddress = null;

            if (NetAddress.TryParse(ipOrHost, out ipAddress))
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                {
                    callback(ipAddress);
                    return;
                }
                throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
            }

            // ok must be a host name
            IPHostEntry entry;

            try
            {
                var addrTask = Dns.GetHostEntryAsync(ipOrHost);
                addrTask.RunSynchronously();
                callback(addrTask.Result.AddressList[0]);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.HostNotFound)
                {
                    //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
                    callback(null);
                }
                else
                {
                    throw;
                }
            }
        }
예제 #4
0
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            registerServiceCallback	= new RegisterServiceCallback(OnRegisterService);
            addPeerCallback			= new AddPeerCallback(OnAddPeer);
            removePeerCallback		= new RemovePeerCallback(OnRemovePeer);
            resolveServiceCallback	= new ResolveServiceCallback(OnResolveService);
            resolveAddressCallback	= new ResolveAddressCallback(OnResolveAddress);
            readMessageCallback		= new ReadMessageCallback(OnReadMessage);

            this.Load += new System.EventHandler(this.Form1_Load);

            this.AcceptButton = button1;
        }
예제 #5
0
        /// <summary>
        /// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname (asynchronous version)
        /// </summary>
        public static void ResolveAsync(string ipOrHost, ResolveAddressCallback callback)
        {
            if (string.IsNullOrEmpty(ipOrHost))
            {
                throw new ArgumentException("Supplied string must not be empty", "ipOrHost");
            }

            ipOrHost = ipOrHost.Trim();

            NetAddress ipAddress = null;

            if (NetAddress.TryParse(ipOrHost, out ipAddress))
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    callback(ipAddress);
                    return;
                }
                throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
            }

            // ok must be a host name
            IPHostEntry entry;

            try
            {
                Dns.BeginGetHostEntry(ipOrHost, delegate(IAsyncResult result)
                {
                    try
                    {
                        entry = Dns.EndGetHostEntry(result);
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.HostNotFound)
                        {
                            //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
                            callback(null);
                            return;
                        }
                        else
                        {
                            throw;
                        }
                    }

                    if (entry == null)
                    {
                        callback(null);
                        return;
                    }

                    // check each entry for a valid IP address
                    foreach (var ipCurrent in entry.AddressList)
                    {
                        if (ipCurrent.AddressFamily == AddressFamily.InterNetwork || ipCurrent.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            callback(ipCurrent);
                            return;
                        }
                    }

                    callback(null);
                }, null);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.HostNotFound)
                {
                    //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
                    callback(null);
                }
                else
                {
                    throw;
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname asynchronously.
        /// </summary>
        public static void ResolveAsync(ReadOnlySpan <char> host, ResolveAddressCallback callback)
        {
            if (host.IsEmpty)
            {
                throw new ArgumentException("Supplied string must not be empty.", nameof(host));
            }

            host = host.Trim();

            if (IPAddress.TryParse(host, out IPAddress? ipAddress))
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork ||
                    ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    callback?.Invoke(ipAddress);
                    return;
                }

                throw new ArgumentException(
                          "This method will not currently resolve other than IPv4 and IPv6 addresses.",
                          nameof(host));
            }

            // ok must be a host name
            IPHostEntry entry;

            try
            {
                Dns.BeginGetHostEntry(host.ToString(), (result) =>
                {
                    try
                    {
                        entry = Dns.EndGetHostEntry(result);
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.HostNotFound)
                        {
                            //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", host.ToString()));
                            callback?.Invoke(null);
                            return;
                        }
                        else
                        {
                            throw;
                        }
                    }

                    if (entry == null)
                    {
                        callback?.Invoke(null);
                        return;
                    }

                    // check each entry for a valid IP address
                    foreach (var ipCurrent in entry.AddressList)
                    {
                        if (ipCurrent.AddressFamily == AddressFamily.InterNetwork ||
                            ipCurrent.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            callback?.Invoke(ipCurrent);
                            return;
                        }
                    }

                    callback?.Invoke(null);
                }, null);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.HostNotFound)
                {
                    //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", host.ToString()));
                    callback?.Invoke(null);
                }
                else
                {
                    throw;
                }
            }
        }