프로젝트

일반

사용자정보

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

markus / CommonLib / DNSHelper.cs @ 92442e4a

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

    
24
                    foreach (IPAddress dnsAdress in dnsAddresses)
25
                    {
26
                        return dnsAdress;
27
                    }
28
                }
29
            }
30
            return null;
31
        }
32

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

    
37
            try
38
            {
39
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
40

    
41
                await Task.Factory.StartNew(() =>
42
                {
43
                    result = Dns.GetHostEntry(ipaddress);
44
                });
45
            }
46
            catch (System.Net.Sockets.SocketException ex)
47
            {
48
                if (ex.SocketErrorCode == System.Net.Sockets.SocketError.HostNotFound)
49
                {
50

    
51
                }
52
            }
53
            catch (Exception ex)
54
            {
55
                System.Diagnostics.Debug.WriteLine(ex.ToString());
56
            }
57

    
58
            return result;
59
        }
60

    
61
        public static IPHostEntry GetHostEntryTask()
62
        {
63
            IPHostEntry result = null;
64

    
65
            try
66
            {
67
                var ipaddress = CommonLib.DNSHelper.GetDnsAdress();
68

    
69
                var task = Task.Factory.StartNew(() =>
70
                {
71
                    result = Dns.GetHostEntry(ipaddress);
72
                });
73

    
74
                task.Wait();
75
            }
76
            catch (Exception ex)
77
            {
78
                if (ex.InnerException is System.Net.Sockets.SocketException)
79
                {
80
                    System.Diagnostics.Debug.WriteLine((ex.InnerException as System.Net.Sockets.SocketException).SocketErrorCode);
81
                }
82
                else
83
                {
84

    
85
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
86
                }
87
            }
88

    
89
            return result;
90
        }
91
    }
92
}
클립보드 이미지 추가 (최대 크기: 500 MB)