Horje
C# Program to Find the IP Address of the Machine

An IP address is known as an Internet Protocol address. It is a unique address that identifies the device over the network. It is almost like a set of rules which govern the data sent over the Internet or through a local network. It helps the Internet to distinguish between routers, computers, websites, etc. In this article, we are going to learn how to find the IP Address of the Machine using C#.

Using GetHostByName() method 

We can find the IP address of the machine using the GetHostByName() method. This method returns the DNS information for the given DNS hostname. When you pass an empty string in this method, then it will return the standard hostname of the local computer.

Syntax:

public static System.Net.IPHostEntry GetHostByName (string hName);

Here, hName is the DNS name of the host.

Approach:

To find the IP address of the machine follow the following steps:

  • Firstly include System.Net.
  • We need to find the name of host to get the IP Address of host. So, the name of host can be retrieved by using the GetHostName() method from the Dns class.
  • By passing the hostname to GetHostByName() method we will get the IP Address.
  • This method returns a structure of type hostent for the specified host name.
  • AddressList[0] gives the ip address and ToString() method is used to convert it to string.

Example:

C#

// C# program to print the IP address of the machine
using System;  
using System.Text;  
using System.Net;
  
class GFG{
      
static void Main(string[] args)  
{
      
    // Get the Name of HOST  
    string hostName = Dns.GetHostName(); 
    Console.WriteLine(hostName);  
      
    // Get the IP from GetHostByName method of dns class.
    string IP = Dns.GetHostByName(hostName).AddressList[0].ToString();  
    Console.WriteLine("IP Address is : " + IP);  
}  
}

Output:

IP Address is : 192.168.122.136

Using GetHostEntry() Method

We can also find the IP address of the machine using the GetHostEntry() method. This method queries DNS server and returns the IP address to the IPHostEntry instance. 

Syntax:

public static System.Net.IPHostEntry GetHostEntry (IPAddress address);

Approach:

To find the IP address of the machine follow the following steps:

  • Firstly include System.Net.
  • We need to find the name of the host to get the IP Address of the host. So, the name of the host can be retrieved by using the GetHostName method from the DNS class.
  • Bypassing the hostname to GetHostEntry( ) method we will get the IP Address.
  • This method returns a structure of type hostent for the specified hostname.
  • AddressList[0] gives the IP address and the ToString() method is used to convert it to string.

Example:

C#

// C# program to print the IP address of the machine
using System;  
using System.Text;  
using System.Net;
  
class GFG{
      
static void Main() 
{
    
    // Getting host name
    string host = Dns.GetHostName();
      
    // Getting ip address using host name
    IPHostEntry ip = Dns.GetHostEntry(host);
    Console.WriteLine(ip.AddressList[0].ToString());
}
}

Output:

IP Address is : 192.168.122.136



Reffered: https://www.geeksforgeeks.org


C#

Related
C# Program to Find Binary Equivalent of an Integer using Recursion C# Program to Find Binary Equivalent of an Integer using Recursion
C# Program to Print the Employees Whose Salary is Between 6000 and 8000 Using LINQ C# Program to Print the Employees Whose Salary is Between 6000 and 8000 Using LINQ
C# Program to Print the Numbers Greater Than 786 in an Integer Array using LINQ C# Program to Print the Numbers Greater Than 786 in an Integer Array using LINQ
C# Program to Print the Employees Whose ID is Greater Than 101 Using LINQ C# Program to Print the Employees Whose ID is Greater Than 101 Using LINQ
C# Program to Estimate the Frequency of the Word “is” in a Sentence C# Program to Estimate the Frequency of the Word “is” in a Sentence

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11