프로젝트

일반

사용자정보

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

markus / CommonLib / DNSHelper.cs @ b7645ccc

이력 | 보기 | 이력해설 | 다운로드 (2.99 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
                if ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
20
                    networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && networkInterface.OperationalStatus == OperationalStatus.Up)
21
                {
22
                    IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
23
                    IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
24

    
25
                    foreach (IPAddress dnsAdress in dnsAddresses)
26
                    {
27
                        if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
28
                            return dnsAdress;
29
                    }
30
                }
31
            }
32
            return null;
33
        }
34

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

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

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

    
61
            System.Diagnostics.Debug.WriteLine("DNS END");
62

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

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