프로젝트

일반

사용자정보

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

markus / CommonLib / DNSHelper.cs @ f3c48db7

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

1
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
                            return dnsAdress;
31
                    }
32
                }
33
            }
34
            return null;
35
        }
36

    
37
        public static async Task<IPHostEntry> GetHostEntryAsync()
38
        {
39
            IPHostEntry result = null;
40

    
41
            await Task.Factory.StartNew(() =>
42
            {
43
                try
44
                {
45
                    var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
46

    
47
                    result = Dns.GetHostEntry(ipaddress);
48
                }
49
                catch (System.Net.Sockets.SocketException ex)
50
                {
51
                    if (ex.SocketErrorCode == System.Net.Sockets.SocketError.HostNotFound)
52
                    {
53
                        System.Diagnostics.Debug.WriteLine("DNS SocketError");
54
                        result = null;
55
                    }
56
                }
57
                catch (Exception ex)
58
                {
59
                    throw ex;
60
                }
61
            });
62

    
63
            System.Diagnostics.Debug.WriteLine("DNS END");
64

    
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
                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
            }
95

    
96
            return result;
97
        }
98
    }
99
}
클립보드 이미지 추가 (최대 크기: 500 MB)