Join 244,196 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,394 people online right now. Registration is fast and FREE... Join Now!
In order to retrieve the local IP address, we call the static method Dns.GetHostByName. This returns an IPHostEntry object, which is a collection of IP addresses, to accommodate multihomed computers, which many are. Element zero in this array is commonly the external IP address for the computer.
found this in book im reading hope it helps
i.e.
CODE
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName()); lblStatus.Text = "My IP address is " + IPHost.AddressList[0].ToString();
This post has been edited by shezzy: 13 Mar, 2007 - 09:50 PM
In order to retrieve the local IP address, we call the static method Dns.GetHostByName. This returns an IPHostEntry object, which is a collection of IP addresses, to accommodate multihomed computers, which many are. Element zero in this array is commonly the external IP address for the computer.
found this in book im reading hope it helps
i.e.
CODE
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName()); lblStatus.Text = "My IP address is " + IPHost.AddressList[0].ToString();
This code works fine if your on the system that is directly connected to the internet, however our situation is different. I am behind a firewall with NAT and I am trying to get my outward facing IP address (the one dhcp assigned to us from our ISP). Internally our IP is 192.168.10.112, (from this current maching). The LAN IP of our router is 192.167.10.254. I want to know the IP address of our router that faces the Internet. Any idea how to do that?
This code works fine if your on the system that is directly connected to the internet, however our situation is different. I am behind a firewall with NAT and I am trying to get my outward facing IP address (the one dhcp assigned to us from our ISP). Internally our IP is 192.168.10.112, (from this current maching). The LAN IP of our router is 192.167.10.254. I want to know the IP address of our router that faces the Internet. Any idea how to do that?
Thanks
Aaron
The simplest way I can think of is to send an HttpWebRequest to whatismyip.com and parse the reply using a regex. Since the ip address is returned in the page title you can use this: (?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>) I know it's quick and dirty but it should work until you find a more permanent solution.
Another idea would be to use a .NET implementation of a traceroute. OK, you can find out the systems local ip using a method in System.Dns. If you run traceroute for an internet ip address, then the first ip hopped which is outside of your local subnet should be the external interface of your router.
In most cases this will correspond to your external internet ip address.
From the looks of it you error isn't actually caused from the code you were provided, as in the code you posted there is no Dispose method in that code. What I think happened is you created a form and just copied the entire snippet into the .cs file, overriding anything that was already there.
I created a new project, opened the .cs file, and inside the
CODE
class Form1 {
}
added the code that you have, minus the namespace and public class IPFinder and it worked fine. So when you create your form, all you need inside the class it creates is
CODE
public static IPAddress GetExternalIp() { string whatIsMyIp = "http://whatismyip.com"; string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); } Regex r = new Regex(getIpRegex); Match m = r.Match(requestHtml); IPAddress externalIp = null; if (m.Success) { externalIp = IPAddress.Parse(m.Value); } return externalIp; }
From the looks of it you error isn't actually caused from the code you were provided, as in the code you posted there is no Dispose method in that code. What I think happened is you created a form and just copied the entire snippet into the .cs file, overriding anything that was already there.
I created a new project, opened the .cs file, and inside the
CODE
class Form1 {
}
added the code that you have, minus the namespace and public class IPFinder and it worked fine. So when you create your form, all you need inside the class it creates is
CODE
public static IPAddress GetExternalIp() { string whatIsMyIp = "http://whatismyip.com"; string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); } Regex r = new Regex(getIpRegex); Match m = r.Match(requestHtml); IPAddress externalIp = null; if (m.Success) { externalIp = IPAddress.Parse(m.Value); } return externalIp; }
And the namespace's also.
hi and thanks for your time i did what you told me to do . and i creat class but i do`nt know how to call this class in for exampel a label ( i mean i want show the ip in a label ) can you plz help . thanks
From the looks of it you error isn't actually caused from the code you were provided, as in the code you posted there is no Dispose method in that code. What I think happened is you created a form and just copied the entire snippet into the .cs file, overriding anything that was already there.
I created a new project, opened the .cs file, and inside the
CODE
class Form1 {
}
added the code that you have, minus the namespace and public class IPFinder and it worked fine. So when you create your form, all you need inside the class it creates is
CODE
public static IPAddress GetExternalIp() { string whatIsMyIp = "http://whatismyip.com"; string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); } Regex r = new Regex(getIpRegex); Match m = r.Match(requestHtml); IPAddress externalIp = null; if (m.Success) { externalIp = IPAddress.Parse(m.Value); } return externalIp; }
And the namespace's also.
hi and thanks for your time i did what you told me to do . and i creat class but i do`nt know how to call this class in for exampel a label ( i mean i want show the ip in a label ) can you plz help . thanks
Hi . i think i finde right answer i will write it down here for anyone looking for get external IP in c#
CODE
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Net;
namespace getWanIp { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
// Add a user agent header in case the requested URI contains a query. client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
string baseurl = "http://checkip.dyndns.org/";
Stream data = client.OpenRead(baseurl); StreamReader reader = new StreamReader(data); string s = reader.ReadToEnd(); data.Close(); reader.Close(); s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString(); MessageBox.Show(s); } } }
.
just creat a new project and go to code mode and copy and paste
And with this page, you don't even need to use a Regex, since the response comes back with just the external IP string, and a much faster response time.
Here is the modified code:
CODE
public static IPAddress GetExternalIp() { string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); }
Replace the given regular expression with this one "\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?", now it doesn’t matter where the IP Address is; once you get the HTML source from the page the Regex.Match will find the first occurrence of a valid IP Address. Hope it helps.
Like this:
CODE
using System; using System.Net; using System.Text; using System.Text.RegularExpressions;
namespace DreamInCode.Snippets { public class IpFinder { public static IPAddress GetExternalIp() { string whatIsMyIp = "http://whatismyip.com"; string getIpRegex = @"\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); } Regex r = new Regex(getIpRegex); Match m = r.Match(requestHtml); IPAddress externalIp = null; if (m.Success) { externalIp = IPAddress.Parse(m.Value); } return externalIp; } } }
QUOTE(dkirkland @ 20 Apr, 2007 - 03:34 PM)
QUOTE(kmdshuaib @ 20 Apr, 2007 - 06:45 AM)
Hi, could u pls tell me how to scratch out the ip address from the title
Try this:
CODE
using System; using System.Net; using System.Text; using System.Text.RegularExpressions;
namespace DreamInCode.Snippets { public class IpFinder { public static IPAddress GetExternalIp() { string whatIsMyIp = "http://whatismyip.com"; string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); } Regex r = new Regex(getIpRegex); Match m = r.Match(requestHtml); IPAddress externalIp = null; if (m.Success) { externalIp = IPAddress.Parse(m.Value); } return externalIp; } } }
I tested it and it seems to work fine. Please let me know if you have problems.
The same approuch with a configuration program has been upload with full code at following link. I'm using the program to have latest remote WAN IP address of the remote location. It is quite useful to me.