showing the external IP-address in C#

How do i do to show my external IP-address in C# ?....

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 163524 Views - Last Post: 15 April 2010 - 11:00 AM Rate Topic: -----

#1 johan_pirate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 06

showing the external IP-address in C#

Post icon  Posted 04 March 2007 - 06:31 AM

Hi everyone! :)

How do i do to get my external IP-address in C# in the easiest way? :rolleyes:

This post has been edited by johan_pirate: 04 March 2007 - 06:34 AM

Is This A Good Question/Topic? 0
  • +

#3 shezzy  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 194
  • Joined: 28-January 07

Re: showing the external IP-address in C#

Posted 13 March 2007 - 10:45 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 :D

i.e.
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
lblStatus.Text = "My IP address is " +
IPHost.AddressList[0].ToString();

This post has been edited by shezzy: 13 March 2007 - 10:50 PM

Was This Post Helpful? 1
  • +
  • -

#4 johan_pirate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 06

Re: showing the external IP-address in C#

Posted 21 March 2007 - 10:52 AM

View Postshezzy, on 13 Mar, 2007 - 10:45 PM, said:

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 :D

i.e.
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
lblStatus.Text = "My IP address is " +
IPHost.AddressList[0].ToString();


Thanx :D
Was This Post Helpful? 0
  • +
  • -

#5 aequasi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-March 07

Re: showing the external IP-address in C#

Posted 22 March 2007 - 11:35 AM

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
Was This Post Helpful? 0
  • +
  • -

#6 dkirkland  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-April 07

Re: showing the external IP-address in C#

Posted 13 April 2007 - 05:22 AM

View Postaequasi, on 22 Mar, 2007 - 11:35 AM, said:

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.

Hope these ideas are of use to you.
Was This Post Helpful? 0
  • +
  • -

#7 kmdshuaib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-April 07

Re: showing the external IP-address in C#

Posted 20 April 2007 - 06:45 AM

Hi,
could u pls tell me how to scratch out the ip address from the title
Was This Post Helpful? 0
  • +
  • -

#8 dkirkland  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-April 07

Re: showing the external IP-address in C#

Posted 20 April 2007 - 04:34 PM

View Postkmdshuaib, on 20 Apr, 2007 - 06:45 AM, said:

Hi,
could u pls tell me how to scratch out the ip address from the title


Try this:
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.

Happy coding :)
Was This Post Helpful? 0
  • +
  • -

#9 StrikerX  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 28-June 07

Re: showing the external IP-address in C#

Posted 28 June 2007 - 02:02 AM

Use GetHostEntry method instead of GetHostByName

			IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
			Console.Write(IPHost.AddressList[0].ToString());


Was This Post Helpful? 1

#10 kingmighty_spades  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 08

Re: showing the external IP-address in C#

Posted 08 February 2008 - 12:45 PM

View Postdkirkland, on 20 Apr, 2007 - 04:34 PM, said:

View Postkmdshuaib, on 20 Apr, 2007 - 06:45 AM, said:

Hi,
could u pls tell me how to scratch out the ip address from the title


Try this:
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.

Happy coding :)


Hi
I did try you code but i get some error can you plz help ?

the erorr are :

Error1: DreamInCode.Snippets.Form1.Dispose(bool)': no suitable method found to override

thanks
Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: showing the external IP-address in C#

Posted 08 February 2008 - 03:21 PM

View Postkingmighty_spades, on 8 Feb, 2008 - 11:45 AM, said:

Hi
I did try you code but i get some error can you plz help ?

the erorr are :

Error1: DreamInCode.Snippets.Form1.Dispose(bool)': no suitable method found to override

thanks

Post your code. We can't help you if we don't know what you are doing.
Was This Post Helpful? 0
  • +
  • -

#12 kingmighty_spades  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 08

Re: showing the external IP-address in C#

Posted 09 February 2008 - 05:05 AM

View Postjayman9, on 8 Feb, 2008 - 03:21 PM, said:

View Postkingmighty_spades, on 8 Feb, 2008 - 11:45 AM, said:

Hi
I did try you code but i get some error can you plz help ?

the erorr are :

Error1: DreamInCode.Snippets.Form1.Dispose(bool)': no suitable method found to override

thanks

Post your code. We can't help you if we don't know what you are doing.



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;
		}
	}
}



error :
Error 1 'DreamInCode.Snippets.Form1.Dispose(bool)': no suitable method found to overrid

This post has been edited by PsychoCoder: 09 February 2008 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

#13 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: showing the external IP-address in C#

Posted 09 February 2008 - 06:48 AM

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

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


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.
Was This Post Helpful? 0
  • +
  • -

#14 kingmighty_spades  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 08

Re: showing the external IP-address in C#

Posted 09 February 2008 - 09:28 AM

View PostPsychoCoder, on 9 Feb, 2008 - 06:48 AM, said:

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

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


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
Was This Post Helpful? 0
  • +
  • -

#15 kingmighty_spades  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-February 08

Re: showing the external IP-address in C#

Posted 09 February 2008 - 04:52 PM

View Postkingmighty_spades, on 9 Feb, 2008 - 09:28 AM, said:

View PostPsychoCoder, on 9 Feb, 2008 - 06:48 AM, said:

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

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


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#

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();
		}

		private void button1_Click(object sender, EventArgs e)
		{
		   
		WebClient client  = new WebClient();

		// 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

good luck and thanks agine :pirate:
Was This Post Helpful? 0
  • +
  • -

#27 mycsharpcorner  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 1
  • Joined: 25-December 08

Re: showing the external IP-address in C#

Posted 25 December 2008 - 11:14 PM

A small note, whatismyip site prefers if you hit their automation page instead of the main page, so it is better to use this link:

http://www.whatismyi...n/n09230945.asp

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:

		
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());
			}
		   
			IPAddress externalIp = IPAddress.Parse(requestHtml);
			return externalIp;
		}



Thanks,
Yousef

Blog: http://www.mycsharpcorner.com
Was This Post Helpful? 1

  • (2 Pages)
  • +
  • 1
  • 2