프로젝트

일반

사용자정보

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

markus / DnsCheckTest / DNSHelper.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (3.78 KB)

1 d60f94ee taeseongkim
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
namespace CommonLib
10
{
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
20
                if ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
21
                    networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
22
                    && networkInterface.OperationalStatus == OperationalStatus.Up)
23
                {
24
                    IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
25
                    IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
26
27
                    foreach (IPAddress dnsAdress in dnsAddresses)
28
                    {
29
                        if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
30
                        {
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
                    }
51
                }
52
            }
53
54
            return null;
55
        }
56
57
        public static async Task<IPHostEntry> GetHostEntryAsync()
58
        {
59
            IPHostEntry result = null;
60
61
            try
62
            {
63
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
64
65
                result = await Dns.GetHostEntryAsync(ipaddress);
66
            }
67
            catch (Exception ex)
68
            {
69
                if (ex.InnerException is System.Net.Sockets.SocketException)
70
                {
71
                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
72
                }
73
                else
74
                {
75
76
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
77
                }
78
            }
79
80
            System.Diagnostics.Debug.WriteLine("DNS END");
81
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
                    result.AddressList = new IPAddress[] { ipaddress };
97
                });
98
99
                task.Wait();
100
            }
101
            catch (Exception ex)
102
            {
103
                if (ex.InnerException is System.Net.Sockets.SocketException)
104
                {
105
                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
106
                }
107
                else
108
                {
109
110
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
111
                }
112
            }
113
114
            return result;
115
        }
116
    }
117
}
클립보드 이미지 추가 (최대 크기: 500 MB)