markus / CommonLib / DNSHelper.cs @ master
이력 | 보기 | 이력해설 | 다운로드 (5.03 KB)
1 | af22332b | ljiyeon | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Net; |
||
5 | using System.Net.NetworkInformation; |
||
6 | using System.Text; |
||
7 | using System.Threading.Tasks; |
||
8 | |||
9 | eb5cdefc | djkim | namespace CommonLib |
10 | af22332b | ljiyeon | { |
11 | public class DNSHelper |
||
12 | { |
||
13 | public static IPAddress GetDnsAdress() |
||
14 | { |
||
15 | NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); |
||
16 | |||
17 | foreach (NetworkInterface networkInterface in networkInterfaces) |
||
18 | { |
||
19 | f87dfb18 | taeseongkim | |
20 | 1137be84 | djkim | if ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet || |
21 | f87dfb18 | taeseongkim | networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) |
22 | a1e2ba68 | taeseongkim | && networkInterface.OperationalStatus == OperationalStatus.Up |
23 | && !networkInterface.Name.Contains("vEthernet") && !networkInterface.Name.Contains("vmware")) |
||
24 | af22332b | ljiyeon | { |
25 | IPInterfaceProperties ipProperties = networkInterface.GetIPProperties(); |
||
26 | IPAddressCollection dnsAddresses = ipProperties.DnsAddresses; |
||
27 | |||
28 | foreach (IPAddress dnsAdress in dnsAddresses) |
||
29 | { |
||
30 | 1137be84 | djkim | if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) |
31 | 366f00c2 | taeseongkim | { |
32 | try |
||
33 | { |
||
34 | var host = Dns.GetHostEntry(dnsAdress); |
||
35 | |||
36 | if (host != null) |
||
37 | { |
||
38 | return dnsAdress; |
||
39 | } |
||
40 | } |
||
41 | catch (Exception ex) |
||
42 | { |
||
43 | if (ex.InnerException is System.Net.Sockets.SocketException) |
||
44 | { |
||
45 | System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode); |
||
46 | } |
||
47 | //return dnsAdress; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | af22332b | ljiyeon | } |
52 | } |
||
53 | } |
||
54 | 366f00c2 | taeseongkim | |
55 | af22332b | ljiyeon | return null; |
56 | } |
||
57 | a1e2ba68 | taeseongkim | public static IPAddress GetClientAdress() |
58 | { |
||
59 | NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); |
||
60 | |||
61 | foreach (NetworkInterface networkInterface in networkInterfaces) |
||
62 | { |
||
63 | if ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet || |
||
64 | networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) |
||
65 | && networkInterface.OperationalStatus == OperationalStatus.Up |
||
66 | && !networkInterface.Name.Contains("vEthernet") && !networkInterface.Name.Contains("vmware")) |
||
67 | { |
||
68 | IPInterfaceProperties ipProperties = networkInterface.GetIPProperties(); |
||
69 | IPAddressCollection dnsAddresses = ipProperties.DnsAddresses; |
||
70 | |||
71 | foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses) |
||
72 | { |
||
73 | if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) |
||
74 | { |
||
75 | return ip.Address; |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return null; |
||
82 | } |
||
83 | cdfb57ff | taeseongkim | |
84 | public static async Task<IPHostEntry> GetHostEntryAsync() |
||
85 | { |
||
86 | IPHostEntry result = null; |
||
87 | |||
88 | 366f00c2 | taeseongkim | try |
89 | cdfb57ff | taeseongkim | { |
90 | 366f00c2 | taeseongkim | var ipaddress = CommonLib.DNSHelper.GetDnsAdress(); |
91 | 566f0526 | taeseongkim | |
92 | 366f00c2 | taeseongkim | result = await Dns.GetHostEntryAsync(ipaddress); |
93 | } |
||
94 | catch (Exception ex) |
||
95 | { |
||
96 | if (ex.InnerException is System.Net.Sockets.SocketException) |
||
97 | 92442e4a | taeseongkim | { |
98 | 366f00c2 | taeseongkim | System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode); |
99 | 92442e4a | taeseongkim | } |
100 | 366f00c2 | taeseongkim | else |
101 | 566f0526 | taeseongkim | { |
102 | 366f00c2 | taeseongkim | |
103 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
104 | 566f0526 | taeseongkim | } |
105 | 366f00c2 | taeseongkim | } |
106 | 566f0526 | taeseongkim | |
107 | System.Diagnostics.Debug.WriteLine("DNS END"); |
||
108 | cdfb57ff | taeseongkim | |
109 | return result; |
||
110 | } |
||
111 | |||
112 | public static IPHostEntry GetHostEntryTask() |
||
113 | { |
||
114 | IPHostEntry result = null; |
||
115 | |||
116 | try |
||
117 | { |
||
118 | var ipaddress = CommonLib.DNSHelper.GetDnsAdress(); |
||
119 | |||
120 | var task = Task.Factory.StartNew(() => |
||
121 | { |
||
122 | result = Dns.GetHostEntry(ipaddress); |
||
123 | }); |
||
124 | |||
125 | task.Wait(); |
||
126 | } |
||
127 | catch (Exception ex) |
||
128 | { |
||
129 | 92442e4a | taeseongkim | if (ex.InnerException is System.Net.Sockets.SocketException) |
130 | { |
||
131 | System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode); |
||
132 | } |
||
133 | else |
||
134 | { |
||
135 | |||
136 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
137 | } |
||
138 | cdfb57ff | taeseongkim | } |
139 | |||
140 | return result; |
||
141 | } |
||
142 | af22332b | ljiyeon | } |
143 | } |