C# program to find IP Address of the client

To find the IP address of the client machine in C#, we use the DNS (Domain Name System) class from the System.Net namespace. This involves getting the hostname first and then resolving it to obtain the associated IP addresses.

Syntax

Following is the syntax to get the hostname −

string hostName = Dns.GetHostName();

Following is the syntax to get IP addresses from hostname −

IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
IPAddress[] addresses = hostEntry.AddressList;

How It Works

The process involves two main steps:

  • Dns.GetHostName() retrieves the hostname of the local computer.

  • Dns.GetHostEntry() resolves the hostname to get an IPHostEntry object containing all associated IP addresses.

  • The AddressList property returns an array of IPAddress objects representing all network interfaces.

IP Address Resolution Process Get Hostname Dns.GetHostName() Resolve to IPs Dns.GetHostEntry() IP Address List 192.168.1.100, ::1, 127.0.0.1

Example

Here's a complete example that displays the client's IP addresses −

using System;
using System.Net;

class Program {
   static void Main() {
      string hostName = string.Empty;
      hostName = Dns.GetHostName();
      Console.WriteLine("Hostname: " + hostName);
      
      IPHostEntry myIP = Dns.GetHostEntry(hostName);
      IPAddress[] address = myIP.AddressList;
      
      for (int i = 0; i < address.Length; i++) {
         Console.WriteLine("IP Address {0}: {1}", i + 1, address[i].ToString());
      }
   }
}

The output of the above code is −

Hostname: DESKTOP-ABC123
IP Address 1: 192.168.1.100
IP Address 2: ::1
IP Address 3: 127.0.0.1

Filtering IPv4 Addresses

If you only want IPv4 addresses, you can filter them using the AddressFamily property −

using System;
using System.Net;
using System.Net.Sockets;

class Program {
   static void Main() {
      string hostName = Dns.GetHostName();
      Console.WriteLine("Hostname: " + hostName);
      
      IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
      
      foreach (IPAddress address in hostEntry.AddressList) {
         if (address.AddressFamily == AddressFamily.InterNetwork) {
            Console.WriteLine("IPv4 Address: " + address.ToString());
         }
      }
   }
}

The output of the above code is −

Hostname: DESKTOP-ABC123
IPv4 Address: 192.168.1.100
IPv4 Address: 127.0.0.1

Common Use Cases

  • Network diagnostics: Identifying the machine's network configuration.

  • Client identification: Logging client information in server applications.

  • Security applications: IP-based access control and monitoring.

Conclusion

Finding the client's IP address in C# is accomplished using Dns.GetHostName() to get the hostname, followed by Dns.GetHostEntry() to resolve all associated IP addresses. This approach works for both IPv4 and IPv6 addresses across different network interfaces.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements