1

I am able to achieve Wi-fi communication between android and PC, by hard coding the IP address of the network connected. But i need to get the IP address of the system connected to a wi-fi network. Iam working on windows platform using C#. So please help me in this regard.

svick
  • 236,525
  • 50
  • 385
  • 514
ragz
  • 99
  • 4
  • 14
  • Take a look at this link: http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c – Mark Kram Feb 28 '12 at 18:26
  • See this: http://stackoverflow.com/questions/2518155/other-than-udp-broadcast-or-multicast-what-other-methods-can-i-use-on-a-wifi-ne – arx Feb 28 '12 at 18:29

2 Answers2

4

This may work for you:

string[] strIP = null;
int count = 0;

IPHostEntry HostEntry = Dns.GetHostEntry((Dns.GetHostName()));
if (HostEntry.AddressList.Length > 0)
{
    strIP = new string[HostEntry.AddressList.Length];
    foreach (IPAddress ip in HostEntry.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            strIP[count] = ip.ToString();
            count++;
        }
    }
}

The problem is, the host could have many IP addresses. This is why the string array is used, it collects them all.

--EDITED by L.B--

Here is the the working version of the code above

var addresses = Dns.GetHostEntry((Dns.GetHostName()))
                    .AddressList
                    .Where(x => x.AddressFamily == AddressFamily.InterNetwork)
                    .Select(x => x.ToString())
                    .ToArray();
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
DIXONJWDD
  • 1,276
  • 10
  • 20
1

For UWP, use this to get your local Ip Address. Updated based on answers by @L.B.

var addresses = Dns.GetHostEntryAsync((Dns.GetHostName()))
                .Result
                .AddressList
                .Where(x => x.AddressFamily == AddressFamily.InterNetwork)
                .Select(x => x.ToString())
                .ToArray();
Syaiful Nizam Yahya
  • 4,196
  • 11
  • 51
  • 71