프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / CommonLib / DNSHelper.cs @ 366f00c2

이력 | 보기 | 이력해설 | 다운로드 (3.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 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 366f00c2 taeseongkim
                        {
31
                            try
32
                            {
33
                                var host = Dns.GetHostEntry(dnsAdress);
34
35
                                if (host != null)
36
                                {
37
                                    return dnsAdress;
38
                                }
39
                            }
40
                            catch (Exception ex)
41
                            {
42
                                if (ex.InnerException is System.Net.Sockets.SocketException)
43
                                {
44
                                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
45
                                }
46
                                //return dnsAdress;
47
                            }
48
                        }
49
            
50 af22332b ljiyeon
                    }
51
                }
52
            }
53 366f00c2 taeseongkim
54 af22332b ljiyeon
            return null;
55
        }
56 cdfb57ff taeseongkim
57
        public static async Task<IPHostEntry> GetHostEntryAsync()
58
        {
59
            IPHostEntry result = null;
60
61 366f00c2 taeseongkim
            try
62 cdfb57ff taeseongkim
            {
63 366f00c2 taeseongkim
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
64 566f0526 taeseongkim
65 366f00c2 taeseongkim
                result = await Dns.GetHostEntryAsync(ipaddress);
66
            }
67
            catch (Exception ex)
68
            {
69
                if (ex.InnerException is System.Net.Sockets.SocketException)
70 92442e4a taeseongkim
                {
71 366f00c2 taeseongkim
                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
72 92442e4a taeseongkim
                }
73 366f00c2 taeseongkim
                else
74 566f0526 taeseongkim
                {
75 366f00c2 taeseongkim
76
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
77 566f0526 taeseongkim
                }
78 366f00c2 taeseongkim
            }
79 566f0526 taeseongkim
80
            System.Diagnostics.Debug.WriteLine("DNS END");
81 cdfb57ff taeseongkim
82
            return result;
83
        }
84
85
        public static IPHostEntry GetHostEntryTask()
86
        {
87
            IPHostEntry result = null;
88
89
            try
90
            {
91
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
92
93
                var task = Task.Factory.StartNew(() =>
94
                {
95
                    result = Dns.GetHostEntry(ipaddress);
96
                });
97
98
                task.Wait();
99
            }
100
            catch (Exception ex)
101
            {
102 92442e4a taeseongkim
                if (ex.InnerException is System.Net.Sockets.SocketException)
103
                {
104
                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
105
                }
106
                else
107
                {
108
109
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
110
                }
111 cdfb57ff taeseongkim
            }
112
113
            return result;
114
        }
115 af22332b ljiyeon
    }
116
}
클립보드 이미지 추가 (최대 크기: 500 MB)