markus / CommonLib / DNSHelper.cs @ 9f55b953
이력 | 보기 | 이력해설 | 다운로드 (3.01 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 | && networkInterface.OperationalStatus == OperationalStatus.Up) |
||
23 | af22332b | ljiyeon | { |
24 | IPInterfaceProperties ipProperties = networkInterface.GetIPProperties(); |
||
25 | IPAddressCollection dnsAddresses = ipProperties.DnsAddresses; |
||
26 | |||
27 | foreach (IPAddress dnsAdress in dnsAddresses) |
||
28 | { |
||
29 | 1137be84 | djkim | if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) |
30 | return dnsAdress; |
||
31 | af22332b | ljiyeon | } |
32 | } |
||
33 | } |
||
34 | return null; |
||
35 | } |
||
36 | cdfb57ff | taeseongkim | |
37 | public static async Task<IPHostEntry> GetHostEntryAsync() |
||
38 | { |
||
39 | IPHostEntry result = null; |
||
40 | |||
41 | 566f0526 | taeseongkim | await Task.Factory.StartNew(() => |
42 | cdfb57ff | taeseongkim | { |
43 | 566f0526 | taeseongkim | try |
44 | cdfb57ff | taeseongkim | { |
45 | 566f0526 | taeseongkim | var ipaddress = CommonLib.DNSHelper.GetDnsAdress(); |
46 | |||
47 | cdfb57ff | taeseongkim | result = Dns.GetHostEntry(ipaddress); |
48 | 566f0526 | taeseongkim | } |
49 | catch (System.Net.Sockets.SocketException ex) |
||
50 | 92442e4a | taeseongkim | { |
51 | 566f0526 | taeseongkim | if (ex.SocketErrorCode == System.Net.Sockets.SocketError.HostNotFound) |
52 | { |
||
53 | System.Diagnostics.Debug.WriteLine("DNS SocketError"); |
||
54 | result = null; |
||
55 | } |
||
56 | 92442e4a | taeseongkim | } |
57 | 566f0526 | taeseongkim | catch (Exception ex) |
58 | { |
||
59 | throw ex; |
||
60 | } |
||
61 | }); |
||
62 | |||
63 | System.Diagnostics.Debug.WriteLine("DNS END"); |
||
64 | cdfb57ff | taeseongkim | |
65 | return result; |
||
66 | } |
||
67 | |||
68 | public static IPHostEntry GetHostEntryTask() |
||
69 | { |
||
70 | IPHostEntry result = null; |
||
71 | |||
72 | try |
||
73 | { |
||
74 | var ipaddress = CommonLib.DNSHelper.GetDnsAdress(); |
||
75 | |||
76 | var task = Task.Factory.StartNew(() => |
||
77 | { |
||
78 | result = Dns.GetHostEntry(ipaddress); |
||
79 | }); |
||
80 | |||
81 | task.Wait(); |
||
82 | } |
||
83 | catch (Exception ex) |
||
84 | { |
||
85 | 92442e4a | taeseongkim | if (ex.InnerException is System.Net.Sockets.SocketException) |
86 | { |
||
87 | System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode); |
||
88 | } |
||
89 | else |
||
90 | { |
||
91 | |||
92 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
93 | } |
||
94 | cdfb57ff | taeseongkim | } |
95 | |||
96 | return result; |
||
97 | } |
||
98 | af22332b | ljiyeon | } |
99 | } |