Click here to Skip to main content
15,867,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to fetch external IP of the user of my Windows Forms Application coded in C# I've tried many things but every code gives out internal IP i.e 192.168.1.1 any help would be appreciated
Posted
Comments
Mike Meinz 23-Oct-13 11:33am    
Your router configuration uses Network Address Translation(NAT). This is handled within the router that is connected to the Internet provider. The PC does not know the external (Internet) address. Solution 1 and Solution 2 both rely on an external server to echo the client address that it sees. Using either of these two techniques will mean that your software is dependent on an external web server run by volunteers.

The code below does not answer your question but this is how I get the IP address of the PC running my application. It is the IP address of the PC on the local area network. It is not the IP address of the router.
string strHostName = System.Net.Dns.GetHostName;
string strIPAddress = null;
System.Net.IPAddress[] objAddressList = System.Net.Dns.GetHostEntry(strHostName).AddressList;
for (x = 0; x <= objAddressList.GetUpperBound(0); x++) {
if (objAddressList(x).AddressFamily == Net.Sockets.AddressFamily.InterNetwork) {
strIPAddress = objAddressList(x).ToString;
}
}

 
Share this answer
 
Comments
Mike Meinz 23-Oct-13 11:56am    
My 5.
Access an external web page that echoes the requesting IP Address and then extract the IP Address from the returned web page.
Screen Scraping!!!
Very clever!
Ranjan.D 23-Oct-13 12:00pm    
Thank you. I hope this would be one of the easiest solution..even i learnt something new :)
Zoltán Zörgő 23-Oct-13 13:00pm    
Yes, this is the only working solution. What I have done when I needed this, is that I added a set of parameter pairs to my application: first the url, and second a regular expression to extract the address (might not be trivial). And the library used this set of providers to get the address. So my suggestion is not to rely on one single provider.
This looks like a good solution, too.

STUN Client[^]
 
Share this answer
 
Comments
Zoltán Zörgő 23-Oct-13 12:56pm    
Interesting, +5! Still, as this relies on a really unique service, it would not be the best choice.

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