C#

네트워크 로컬 아이피, 글로벌 아이피 확인

아스C# 2021. 1. 21. 18:35
반응형

 

로컬 아이피를 가져오는 함수1

 

using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Diagnostics;
using System;
using System.Linq;
using System.Net;

 

 public static IPAddress LocalIPAddress1()
      {
         if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
         {
            return null;
         }

         IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());

         return host
             .AddressList
             .FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
      }

 

 

 

 

 

 

 

로컬 아이피 가져오는 함수2

 

     public static string LocalIPAddress2()
      {
         string localIP;
         using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
         {
            socket.Connect("8.8.8.8", 65530);
            IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
            localIP = endPoint.Address.ToString();
            return localIP;
         }
      }

 

 

 로컬 아이피 가져오는 함수3

 

      public static string LocalIPAddress3() //lip
      {
         var host = Dns.GetHostEntry(Dns.GetHostName());

         foreach (var ip in host.AddressList)
         {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
               return ip.ToString();
            }
         }
         throw new Exception("No network adapters with an IPv4 address in the system!");
      }

 

 

 

 

 

 

 

 

글로벌 아이피 가져오는 함수

 

 	public static string GlobalIPAddress() //gip 
      {
         string externalip = new WebClient().DownloadString("http://ipinfo.io/ip").Trim(); //http://icanhazip.com

         if (String.IsNullOrWhiteSpace(externalip))
         {
            externalip = LocalIPAddress3();//null경우 Get Internal IP를 가져오게 한다.
         }

         return externalip;
      }

 

맥어드레스 가져오는 함수

 

      public static string MacAddress()
      {
         try
         {
            return NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString();

         }
         catch (Exception ex)
         {
            return "";
         }
      }

 

 

인터넷 연결 여부 체크

 

      public static bool IsInternetConnected()
      {
         const string NCSI_TEST_URL = "http://www.msftncsi.com/ncsi.txt";
         const string NCSI_TEST_RESULT = "Microsoft NCSI";
         const string NCSI_DNS = "dns.msftncsi.com";
         const string NCSI_DNS_IP_ADDRESS = "131.107.255.255";

         try
         {
            // Check NCSI test link
            var webClient = new WebClient();
            string result = webClient.DownloadString(NCSI_TEST_URL);
            if (result != NCSI_TEST_RESULT)
            {
               return false;
            }

            // Check NCSI DNS IP
            var dnsHost = Dns.GetHostEntry(NCSI_DNS);
            if (dnsHost.AddressList.Count() < 0 || dnsHost.AddressList[0].ToString() != NCSI_DNS_IP_ADDRESS)
            {
               return false;
            }
         }
         catch (Exception ex)
         {
            Debug.WriteLine(ex);
            return false;
         }

         return true;
      }

 

 

 

반응형