Click here to Skip to main content
15,611,246 members
Articles / Programming Languages / C#
Posted 7 Dec 2015

Tagged as


10 bookmarked

Retrieve IPv4 and IPv6 IP Addresses from a Server

Rate me:
Please Sign up or sign in to vote.
4.00/5 (11 votes)
9 Dec 2015CPOL1 min read
Retrieve both IPv4 and IPv6 from Windows 7 / 8 / 10 / server 2012


A simple tip to retrieve IPv4 / IPv6 address from any machine.


Internet Protocol version 6 (IPv6) is a mandatory part of Windows Vista and later versions. So when you ping a server greater than Windows 7, it might be responding on IPv6 or they are resolving IPv6. If they are resolving to IPv6, they might IPv6 addresses registered in DNS.

In cases when general ping commands resolve to IPv6, there might be a need for IPv4 as well as most of current manipulations require IPv4. Here is a simple trick in C# to achieve that differentiation.

Using the Code

Using System.Net namespace following can be easily achieved.

foreach (IPAddress ipaddr in Dns.GetHostAddresses(Environment.MachineName))
           // Get all the possible IP addresses from the hostname
           // GetHostAddresses() returns array of IPAddress
           // loop thru all addresses one by one
               // IP Addresses have family. This is defined as AddressFamily in System.Net.Sockets
               // In simple words each family is a type of network protocol like IPv4, IPv6,
       // AppleTalk, CCITT etc.
               // Example :

               if (ipaddr.AddressFamily ==
       System.Net.Sockets.AddressFamily.InterNetwork) // InterNetwork =
                               //Address Family for IP version 4
                   Console.WriteLine("IP V4 Address : " + ipaddr.ToString());
               else if (ipaddr.AddressFamily ==
           System.Net.Sockets.AddressFamily.InterNetworkV6) // InterNetwork6 =
                               //Address Family for IP version 6
                   Console.WriteLine("IP V6 Address : " + ipaddr.ToString());

Code Explanation

  1. Get all allotted IP addresses of the machine using DNS name.
  2. Once we get it, parse through each IP to check its network protocol property, i.e., its AddressFamily
  3. If we match IPv4 and IPv6 correspondingly assign values.
  4. Replace Environment.MachineName with any remote machine as needed.
  5. Please refer for enum details on AddressFamily which determines IPv4 or IPv6.

Why this Code ??

To find the MAC address of a server, using IPv4 there are tons of ways, but with IPv6 it's complex. So this comes handy when trying to find various network properties especially for MAC

Points of Interest

I searched a lot in various websites to get IPv4 from a Windows 10 machine, which by default was returning IPv6. Hence, I am posting here so that it may be useful to others.

If you wanted to test, if machine is reachable or not at first place, please use System.Net.NetworkInformation.Ping and System.Net.NetworkInformation.PingReply as needed.


  • 1st revision: 07-Dec-2015


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionGood Tip Pin
Member 813201010-Dec-15 16:25
Member 813201010-Dec-15 16:25 
AnswerRe: Good Tip Pin
rvjagadheesh11-Dec-15 2:23
professionalrvjagadheesh11-Dec-15 2:23 
Suggestiondon't compare enums as string Pin
sx20088-Dec-15 10:22
sx20088-Dec-15 10:22 
GeneralRe: don't compare enums as string Pin
rvjagadheesh9-Dec-15 5:49
professionalrvjagadheesh9-Dec-15 5:49 
GeneralMy vote of 2 Pin
GerVenson8-Dec-15 1:58
professionalGerVenson8-Dec-15 1:58 
GeneralRe: My vote of 2 Pin
rvjagadheesh9-Dec-15 5:56
professionalrvjagadheesh9-Dec-15 5:56 
GeneralRe: My vote of 2 Pin
GerVenson9-Dec-15 6:56
professionalGerVenson9-Dec-15 6:56 
GeneralRe: My vote of 2 Pin
rvjagadheesh9-Dec-15 7:06
professionalrvjagadheesh9-Dec-15 7:06 
Questionhow bored from 1-10 you were when you wrote this 'article' ? Pin
Member 80030638-Dec-15 1:30
Member 80030638-Dec-15 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.