locked
Detecting a router's external IP RRS feed

  • Question

  • Hello everybody, I'm just getting started in the realm of networking w/ the .NET framework.

    I'm coding my app in C#, it's just a simple command line server that will listen for commands from an external client.

    Right now, I'm just trying to get the server, when started, to print out the local machine's IP.

    However, I want it to print out the address as seen by computers not on my network. So, instead of 192.168.1.XXX, I want it to give me what a website like http://whatismyipaddress.com/ would return.

    Thanks in advance!
    Tuesday, November 7, 2006 3:43 AM

Answers

  • There are many ways of detecting external IP of router. One of the easiest if using "web services".
    However, this method has risk of using because the web service may change anytime and therefore this method will no more applicable.
    I will talk about how to request external IP from "external DNS" in other time. Today i talk about the easiest way first.
    
    System.Net.HttpWebRequest request =
    (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.what
    ismyip.org/");
    
    request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE
    6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    
    System.Net.HttpWebResponse response =
    	(System.Net.HttpWebResponse)request.GetResponse();
    
    string myExternalIP = string.Empty;
    
    using (System.IO.StreamReader reader = new
    	StreamReader(response.GetResponseStream()))
    {
    
    	myExternalIP = reader.ReadToEnd();
    	reader.Close();
    }
    
    response.Close();
    
    MessageBox.Show(myExternalIP);
     
     
    Wednesday, November 22, 2006 1:02 PM
  • To answer the question above about obtaining your internal IP (ie. 192.168.x.x) see the System.Net.NetworkInformation namespace.  More specifically see this MSDN page: http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties.aspx
    Wednesday, November 22, 2006 9:01 PM

All replies

  • It is not generally possible to detect the external facing IPAddress from machine
    behind the router. You have to depend on external entities to tell you what your external IPAddress is...

    Tuesday, November 7, 2006 5:35 AM
    Moderator
  • Well, I suppose that's true, but could you at least tell me how to detect my IP behind the router then.

    I know it's 192.168.1.111 in my case, but how can I detect that through C# coding?
    Tuesday, November 7, 2006 9:23 PM
  • You would have to use HttpWebRequest and Parse the response text to a label on your form.
    Tuesday, November 7, 2006 9:55 PM
  • Wait, I don't see how that applies to obtaining my machine's IP.

    Excuse me if this is really noobish, but I need some explanation.

    Thanks
    Wednesday, November 8, 2006 5:41 AM
  •  Sushisource wrote:
    Wait, I don't see how that applies to obtaining my machine's IP.

    Excuse me if this is really noobish, but I need some explanation.

    Thanks

    MSDN got that explaination and working sample in itself. See MSDN for HttpWebRequest and HttpWebResponse. It's really 3-4 lines of code and its done. Atleast give it a try. I hope you'll do and understand it within a few minutes!

    Best Regads and Best of Luck!

    Rizwan

    Wednesday, November 8, 2006 5:46 AM
  • Yes, I've been looking though it, and I think I understand it that I basically retrieve the HTML of the web page, and then use a series of string searches to obtain my IP from it.

    Thanks
    Wednesday, November 8, 2006 6:03 AM
  • There are many ways of detecting external IP of router. One of the easiest if using "web services".
    However, this method has risk of using because the web service may change anytime and therefore this method will no more applicable.
    I will talk about how to request external IP from "external DNS" in other time. Today i talk about the easiest way first.
    
    System.Net.HttpWebRequest request =
    (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.what
    ismyip.org/");
    
    request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE
    6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    
    System.Net.HttpWebResponse response =
    	(System.Net.HttpWebResponse)request.GetResponse();
    
    string myExternalIP = string.Empty;
    
    using (System.IO.StreamReader reader = new
    	StreamReader(response.GetResponseStream()))
    {
    
    	myExternalIP = reader.ReadToEnd();
    	reader.Close();
    }
    
    response.Close();
    
    MessageBox.Show(myExternalIP);
     
     
    Wednesday, November 22, 2006 1:02 PM
  • To answer the question above about obtaining your internal IP (ie. 192.168.x.x) see the System.Net.NetworkInformation namespace.  More specifically see this MSDN page: http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties.aspx
    Wednesday, November 22, 2006 9:01 PM
  • Hi

    this is a easy way

    string ip = new WebClient().DownloadString(http://www.whatismyip.com/automation/n09230945.asp);

    • Proposed as answer by Anastaway Saturday, March 13, 2010 5:56 PM
    Thursday, November 22, 2007 12:19 AM
  • If you have a web server yourself, the easiest way for one of your programs to get its WAN IP address is to do what Aymaneez suggests, but to load the address from your own web server. The problem with using someone else's is that they often change the format of their page, or have more than one IP address on the page (both in the HTML and the javascript), making it tedious to extract it using a regular expression.

     

    If you have your own webserver, you can just request a page from it using your program, and that page can return the address in a very easily readable and controlled manner. Below I give the code to make an IPServer.aspx which returns only the IP address of the calling program with no other text or tags. Then just do what Aymaneez suggests, but use your own url.

     

    Here is IPServer.aspx (it is just one line):

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="IPServer.aspx.cs" Inherits="IPServer" %>

     

    And here is IPServer.aspx.cs:

     

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

     

    public partial class IPServer : System.Web.UI.Page {

     

    protected void Page_Load(object sender, EventArgs e) {

     

    Response.Write(Request.ServerVariables["REMOTE_ADDR"]);

    }

    }

     

    I hope that helps.

     

     

    Thursday, November 22, 2007 6:49 AM