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