Example #1
0
        /// <summary>
        /// Determines whether it is possible to connect to the specified host.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="port">The port.</param>
        /// <returns>
        ///     <c>true</c> if this instance can connect the specified host; otherwise, <c>false</c>.
        /// </returns>
        /// <example>
        /// if (NetworkHelper.CanConnect("www.Microsoft.com", 80))
        /// {
        ///     // ... do something with the connection
        /// }
        /// </example>
        public static bool CanConnect(string host, int port)
        {
            bool result;

            try
            {
                string             localHostname   = Dns.GetHostName();
                IPHostEntry        localHostEntry  = Dns.GetHostEntry(localHostname);
                IPHostEntry        remoteHostEntry = Dns.GetHostEntry(host);
                NetworkInterface[] networks        = NetworkInterface.GetAllNetworkInterfaces();
                NetworkInterface[] array           = networks;
                for (int i = 0; i < array.Length; i++)
                {
                    NetworkInterface network = array[i];
                    if (network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType != NetworkInterfaceType.Loopback && network.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
                    {
                        IPInterfaceProperties       interfaceProperties = network.GetIPProperties();
                        List <IPAddressInformation> adapterAddressList  = NetworkHelper.GetAllAddressesForAdapter(interfaceProperties);
                        IPAddress[] addressList = localHostEntry.AddressList;
                        for (int j = 0; j < addressList.Length; j++)
                        {
                            IPAddress localHostAddress = addressList[j];
                            if (NetworkHelper.CanConnectFromAddress(localHostAddress, adapterAddressList, remoteHostEntry, port))
                            {
                                result = true;
                                return(result);
                            }
                        }
                    }
                }
                result = false;
            }
            catch (Exception)
            {
                result = false;
            }
            return(result);
        }
Example #2
0
 /// <summary>
 /// Determines whether it is possible to connect to the specified host.
 /// </summary>
 /// <param name="host">The host.</param>
 /// <returns>
 ///     <c>true</c> if this instance can connect the specified host; otherwise, <c>false</c>.
 /// </returns>
 /// <example>
 /// if (NetworkHelper.CanConnect("www.Microsoft.com"))
 /// {
 /// // ... do something with the connection
 /// }
 /// </example>
 /// <remarks>Port 80 is used to make the connection attempt.</remarks>
 public static bool CanConnect(string host)
 {
     return(NetworkHelper.CanConnect(host, 80));
 }