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.
Asked
Active
Viewed 8,024 times
1
-
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 Answers
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`HosyEntry`, use of `strIP` without initializing. etc. Did you try to compile&run your code? – L.B Feb 28 '12 at 19:09
-
yep - sorry. I was away from VS when I wrote this (on my phone). +1 for good edit – DIXONJWDD Feb 28 '12 at 19:22
-
1This doesn't get the connected wifi IP address but rather your local machines ip – James Heffer Jan 07 '19 at 08:14
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