|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking;
using Windows.Networking.Connectivity;
using Windows.Networking.Sockets;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace Network_Helper
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
#region Network Helper Methods
public static bool GetHostName(string ipOrhost, out HostName hostName)
{
HostName internalHostName = null;
bool isValidHost = false;
try
{
internalHostName = new HostName(ipOrhost);
isValidHost = true;
}
catch (Exception ex)
{
isValidHost = false;
internalHostName = null;
// Exception Invalid Host name
}
finally
{
hostName = internalHostName;
}
return isValidHost;
}
public static async Task<string> ResolveDNS(string remoteHostName)
{
if (string.IsNullOrEmpty(remoteHostName))
return string.Empty;
string ipAddress = string.Empty;
try
{
IReadOnlyList<EndpointPair> data = await DatagramSocket.GetEndpointPairsAsync(new HostName(remoteHostName), "0");
if (data != null && data.Count > 0)
{
foreach (EndpointPair item in data)
{
if (item != null && item.RemoteHostName != null && item.RemoteHostName.Type == HostNameType.Ipv4)
{
return item.RemoteHostName.CanonicalName;
}
}
}
}
catch (Exception ex)
{
ipAddress = ex.Message;
}
return ipAddress;
}
public static async Task<bool> CompareIpAddress(string baseAddress, string secondAddress)
{
bool isequal = false;
if (string.IsNullOrWhiteSpace(baseAddress) || string.IsNullOrWhiteSpace(secondAddress))
return isequal;
string ipAddressBase = await ResolveDNS(baseAddress);
string ipAddressSecond = await ResolveDNS(secondAddress);
if (string.IsNullOrWhiteSpace(ipAddressBase) || string.IsNullOrWhiteSpace(ipAddressSecond))
return isequal;
if (ipAddressBase.Equals(ipAddressSecond))
isequal = true;
return isequal;
}
#endregion
#region Click events
private async void btnResolve_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtDnsIP.Text))
{
try
{
lblResolveAddress.Text = await ResolveDNS(txtDnsIP.Text);
}
catch (Exception ex)
{
lblResolveAddress.Text = "Invalid";
}
}
}
private async void btnCompare_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtBaseAddress.Text) &&
!string.IsNullOrEmpty(txtComAddress.Text))
{
try
{
bool result = await CompareIpAddress(txtBaseAddress.Text, txtComAddress.Text);
if (result)
{
lblResullt2.Text = "Addresses are Same";
}
else
{
lblResullt2.Text = "Addresses Differ ";
}
}
catch (Exception ex)
{
}
}
}
private void btnShowInfo_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtUrl.Text))
{
try
{
HostName hostInfo = null;
if (GetHostName(txtUrl.Text, out hostInfo))
{
if (hostInfo != null)
{
StringBuilder info = new StringBuilder();
info.AppendLine(string.Format("CanonicalName - {0}", hostInfo.CanonicalName));
info.AppendLine(string.Format("DisplayName - {0}", hostInfo.DisplayName));
info.AppendLine(string.Format("RawName - {0}", hostInfo.RawName));
info.AppendLine(string.Format("Type - {0}", hostInfo.Type.ToString()));
lblResultInfo.Text = info.ToString();
}
}
else
{
lblResultInfo.Text = "Error Hostname";
}
}
catch (Exception ex)
{
}
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.