프로젝트

일반

사용자정보

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

markus / CommonLib / DNSHelper.cs @ 37bd9e47

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