Skip to content

Commit 3da4bba

Browse files
committed
* Added host name attribute
* Added some documentation
1 parent 8456403 commit 3da4bba

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

Advanced PortChecker/Classes/LvCheck.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
/// </summary>
66
public class LvCheck
77
{
8-
// ReSharper disable once UnusedAutoPropertyAccessor.Global
98
public string Address { get; set; }
10-
// ReSharper disable once UnusedAutoPropertyAccessor.Global
119
public int Port { get; set; }
12-
// ReSharper disable once UnusedAutoPropertyAccessor.Global
10+
public string HostName { get; set; }
1311
public string Type { get; set; }
14-
// ReSharper disable once UnusedAutoPropertyAccessor.Global
1512
public string Description { get; set; }
1613
}
1714
}

Advanced PortChecker/Classes/PortChecker.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Net;
34
using System.Net.Sockets;
45
using System.Threading.Tasks;
56

@@ -64,6 +65,7 @@ await Task.Run(() =>
6465
{
6566
Address = address,
6667
Port = i,
68+
HostName = GetMachineNameFromIpAddress(address),
6769
Type = "TCP",
6870
Description = IsTcpOpen(address, i, timeout) ? "Open" : "Closed"
6971
};
@@ -104,6 +106,7 @@ await Task.Run(() =>
104106
{
105107
Address = address,
106108
Port = i,
109+
HostName = GetMachineNameFromIpAddress(address),
107110
Type = "UDP",
108111
Description = IsUdpOpen(address, i, timeout) ? "Open" : "Closed"
109112
};
@@ -172,5 +175,25 @@ private static bool IsUdpOpen(string address, int port, int timeout)
172175
}
173176
return false;
174177
}
178+
179+
/// <summary>
180+
/// Get the Host name by IP address
181+
/// </summary>
182+
/// <param name="ipAdress">The IP address that needs to be resolved to a host name</param>
183+
/// <returns>The host name of an IP address</returns>
184+
private static string GetMachineNameFromIpAddress(string ipAdress)
185+
{
186+
string machineName = string.Empty;
187+
try
188+
{
189+
IPHostEntry hostEntry = Dns.GetHostEntry(ipAdress);
190+
machineName = hostEntry.HostName;
191+
}
192+
catch (Exception)
193+
{
194+
// Machine not found...
195+
}
196+
return machineName;
197+
}
175198
}
176199
}

Advanced PortChecker/Windows/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<GridView>
184184
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" />
185185
<GridViewColumn Header="Port" DisplayMemberBinding="{Binding Port}" />
186+
<GridViewColumn Header="Host name" DisplayMemberBinding="{Binding HostName}" />
186187
<GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}" />
187188
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" />
188189
</GridView>

0 commit comments

Comments
 (0)