프로젝트

일반

사용자정보

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

markus / CommonLib / DNSHelper.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (5.03 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
                    && !networkInterface.Name.Contains("vEthernet") && !networkInterface.Name.Contains("vmware"))
24
                {
25
                    IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
26
                    IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
27

    
28
                    foreach (IPAddress dnsAdress in dnsAddresses)
29
                    {
30
                        if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
31
                        {
32
                            try
33
                            {
34
                                var host = Dns.GetHostEntry(dnsAdress);
35

    
36
                                if (host != null)
37
                                {
38
                                    return dnsAdress;
39
                                }
40
                            }
41
                            catch (Exception ex)
42
                            {
43
                                if (ex.InnerException is System.Net.Sockets.SocketException)
44
                                {
45
                                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
46
                                }
47
                                //return dnsAdress;
48
                            }
49
                        }
50
            
51
                    }
52
                }
53
            }
54

    
55
            return null;
56
        }
57
        public static IPAddress GetClientAdress()
58
        {
59
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
60

    
61
            foreach (NetworkInterface networkInterface in networkInterfaces)
62
            {
63
                if ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
64
                    networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
65
                    && networkInterface.OperationalStatus == OperationalStatus.Up
66
                    && !networkInterface.Name.Contains("vEthernet") && !networkInterface.Name.Contains("vmware"))
67
                {
68
                    IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
69
                    IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
70

    
71
                    foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses)
72
                    {
73
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
74
                        {
75
                            return ip.Address;
76
                        }
77
                    }
78
                }
79
            }
80

    
81
            return null;
82
        }
83

    
84
        public static async Task<IPHostEntry> GetHostEntryAsync()
85
        {
86
            IPHostEntry result = null;
87

    
88
            try
89
            {
90
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
91

    
92
                result = await Dns.GetHostEntryAsync(ipaddress);
93
            }
94
            catch (Exception ex)
95
            {
96
                if (ex.InnerException is System.Net.Sockets.SocketException)
97
                {
98
                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
99
                }
100
                else
101
                {
102

    
103
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
104
                }
105
            }
106

    
107
            System.Diagnostics.Debug.WriteLine("DNS END");
108

    
109
            return result;
110
        }
111

    
112
        public static IPHostEntry GetHostEntryTask()
113
        {
114
            IPHostEntry result = null;
115

    
116
            try
117
            {
118
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
119

    
120
                var task = Task.Factory.StartNew(() =>
121
                {
122
                    result = Dns.GetHostEntry(ipaddress);
123
                });
124

    
125
                task.Wait();
126
            }
127
            catch (Exception ex)
128
            {
129
                if (ex.InnerException is System.Net.Sockets.SocketException)
130
                {
131
                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
132
                }
133
                else
134
                {
135

    
136
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
137
                }
138
            }
139

    
140
            return result;
141
        }
142
    }
143
}
클립보드 이미지 추가 (최대 크기: 500 MB)