C# Program to Find the IP Address of the Machine
Last Updated :
19 Oct, 2021
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
Similar Reads
How to get IP Address of clients machine in PHP ? An IP address is used to provide an identity to a device connected to a network. IP address stands for Internet Protocol address. An IP address allows to track the activities of a user on a website and also allows the location of different devices that are connected to the network to be pinpointed a
2 min read
Program to convert Byte array to IP Address Given a Byte Array, convert it to the format of IP Address. Examples: Input : {16, 16, 16, 16} Output : 16.16.16.16Input : {172, 31, 102, 14} Output : 172.31.102.14Byte arrays: A byte is a collection of bits (8). Byte arrays are arrays of contiguous bytes and can be used to store binary information.
3 min read
How to Find Your IP Address? Finding your IP address is a simple but also an important task for anyone using the internet. Whether youâre troubleshooting network issues, setting up a new device, or securing your online activity, knowing your IP address is a key step.In this article, weâll show you how to find your IP address on
6 min read
C# Program to Get the Machine Name or Host Name Using Environment Class Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environme
1 min read
How to Find a Loopback Address? In a Computer Network, the Loopback Address is an important address that is used for testing and troubleshooting the network connectivity on the local machine. this address provides a path for a computer system to transfer network packets to itself without involving any external networks. Loopback a
4 min read
How to Find Your Local IP Address in Debian 11 The IP address is the specific number that identifies the devices on the network. This IP address will help the other devices to find and communicate with your device. When you connect your device to a a computer or phone to the local network such as your home and office network it immediately assig
4 min read