Click here to Skip to main content
15,920,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hot to get the right IP address? I have tied this:
C#
string strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
MessageBox.Show("IP Address {0}: {1} " + addr[i].ToString());
}

but it shows me all the numbers that are not the IP. I only need to show the right one.
Posted
Comments
[no name] 11-Apr-13 11:46am    
What are the numbers that it is showing you? What would be the "right" one?
Sergey Alexandrovich Kryukov 11-Apr-13 11:48am    
AddressList really gives an array of IP addresses. This is correct result. No one it more "right" than another. :-)
—SA
[no name] 11-Apr-13 13:55pm    
What does that have to do with anything? Look again at his message box string. Do you really think that that would display an IP address as he wants?
Sergey Alexandrovich Kryukov 11-Apr-13 15:17pm    
Oh, no! :-)
—SA

If you mean "It gives me an IP address which is in the range '192.x.x.x' and does not match the number WhatsMyIP.com tells me" then it's not that simple.

The WhatsMyIP.com number is your internet address, which is actually the internet IP address of your router - it does not go any further than that. On the PC side of the router, you use different IP addresses assigned by your DHCP server (normally the router again) and which are normally in the range 192.172.x.x. Your PC never needs to know the internet IP address as the messgaes are all routed (and the IP addresses changed) by the router when it feeds your requests to the internet and returns the results.

If you want the actual internet IP address, then you either have to query the router directly (and all manufacturers give this differently, so I don't recommend it) or use an IP service on teh internet to find it - such as WhatsMyIP.com.
 
Share this answer
 
Comments
Member 4347041 11-Apr-13 12:19pm    
It shows me 4 of them, only one looks like IP, three of them are like this:
dr44::a7e2:7f7i:6f32:716g%19
And now i need the code to only look for that one that is IP, like 192.x.x.x
OriginalGriff 11-Apr-13 12:34pm    
Ah! The others are IPV6 addresses, rather than the older IPV4 address you are used to. They are all IP addresses, but if you are really looking for your internal network IPV4 address, then add the following:
if (addr[i].AddressFamily == AddressFamily.InterNetwork)
{
MessageBox.Show(string.Format("Bingo!\n{0}:{1}", i, addr[i]));
}

Member 4347041 12-Apr-13 4:13am    
Where is AddressFamily defined?
OriginalGriff 12-Apr-13 4:23am    
System.Net.Sockets
http://msdn.microsoft.com/en-us/library/system.net.sockets.addressfamily.aspx

If you aren't sure where something is defined, put your cursor in the underlined name in VS, and a small blue line will appear at the beginning.
Put your mouse over the line, and a drop down will appear. Open the drop down, and it will give you options to add the namespace info by generating the using statement for you, or explicitly adding the namespace to the single line use. Handy!
Member 4347041 12-Apr-13 4:38am    
Thanks. Where can i find all (list of) those libraries (using) and what their are containing?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900