Home  >  Article  >  Backend Development  >  How to get ip address in C#?

How to get ip address in C#?

PHPz
PHPzforward
2023-09-01 08:09:04767browse

How to get ip address in C#?

#The IP (Internet Protocol) address is the address of your network hardware. It helps connect your computer to other devices on the network and around the world. IP addresses are made up of numbers or characters.

Every device connected to an internet connection has a unique IP address, which means billions of IP addresses are needed. The new version of IP, IPv6, meets this requirement.

Private IP Address

A private IP address is the address of a device connected to your home or home. business network. If you have multiple different devices connected to one ISP (Internet Service Provider), then all of your devices will have unique private IP addresses. This IP address is not accessible from devices outside your home or business network.

For example: 192.168.1.1

Example

class Program{
   static void Main(string[] args){
      string IPAddress = GetIPAddress();
      System.Console.WriteLine(IPAddress);
      Console.ReadLine();
   }
   public static string GetIPAddress(){
      string IPAddress = string.Empty;
      IPHostEntry Host = default(IPHostEntry);
      string Hostname = null;
      Hostname = System.Environment.MachineName;
      Host = Dns.GetHostEntry(Hostname);
      foreach (IPAddress IP in Host.AddressList){
         if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){
            IPAddress = Convert.ToString(IP);
         }
      }
      return IPAddress;
   }
}

Output

192.168.1.1

The above is the detailed content of How to get ip address in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete