Re: Here is another implementation using IPAddress and IPHostEntry classes
From: 2G (2G_at_pandora.be)
Date: 03/19/04
- Next message: Dmitry Karneyev: "Crystal Reports Viewer"
- Previous message: Dennis Myrén: "Re: switch or if?"
- In reply to: Anonieko Ramos: "Re: Here is another implementation using IPAddress and IPHostEntry classes"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 19 Mar 2004 08:40:46 GMT
Hi,
It will depend on your isp. I'm using Telenet (Belgium) so for me,
retrieving my routers ip thru a webpage will not work because the webpage
will display the ip address of the proxy from my isp.
I'm also searching for a solution to retrieve my routers public ip but
haven't found one yet.
Grtz
"Anonieko Ramos" <anonieko@hotmail.com> wrote in message
news:562cba23.0403182223.3d158b9f@posting.google.com...
> Ram,
> If you are in an intranet, this program only shows your
> intranet IP address (e.g. 192.168.xxx.xxx ). I tried
> both programs. The solution by ElreyRonald will correctly
> print the ROUTER address ( bec. it is the IP as 'seen from outside')
>
>
>
>
> "Ram Dash" <rampr2@hotmail.com> wrote in message
news:<ujia$EWDEHA.684@tk2msftngp13.phx.gbl>...
> > I got this from http://www.dotnet247.com/247reference/msgs/1/7651.aspx.
Here
> > is it:
> >
> > using System;
> >
> > using System.Net;
> >
> > namespace ConsoleApplication1
> >
> > {
> >
> > /// <summary>
> >
> > /// Summary description for Class1.
> >
> > /// </summary>
> >
> > class Class1
> >
> > {
> >
> > /// <summary>
> >
> > /// The main entry point for the application.
> >
> > /// </summary>
> >
> > [STAThread]
> >
> > static void Main(string[] args)
> >
> > {
> >
> > string host;
> >
> > host = Dns.GetHostName();
> >
> > Console.WriteLine(host);
> >
> > IPHostEntry i = Dns.GetHostByName(host);
> >
> > foreach (IPAddress a in i.AddressList)
> >
> > {
> >
> > Console.WriteLine(a);
> >
> > }
> >
> > Console.ReadLine();
> >
> > }
> >
> > }
> >
> > }
> >
> > "Anonieko Ramos" <anonieko@hotmail.com> wrote in message
> > news:562cba23.0403181804.29a0fc05@posting.google.com...
> > > I found this code from ElreyRonald. It is a console but
> > > works by referencing another website.
> > >
> > >
> > >
> > >
> > > //--------------------------------------------------------------
> > > // A console application to print IP Address of the current
> > > // workstation (or its router's IP )
> > > //
> > > // Two things are demonstrated here:
> > > // (1) How to get a web page html and
> > > // (2) How to use regular expression to search within a string.
> > > //
> > > // Written by ElreyRonald V. [lambda326@hotmail.com] 2004
> > > //--------------------------------------------------------------
> > >
> > >
> > > using System;
> > > using System.Text.RegularExpressions;
> > > using System.Text;
> > > using System.Net;
> > > using System.IO;
> > >
> > >
> > > namespace Network.Utilities
> > > {
> > >
> > > class GetIp
> > > {
> > > [STAThread]
> > > static void Main(string[] args)
> > > {
> > > // use a website which displays your IP
> > > string urlString = "http://www.getmyipaddress.com";
> > >
> > > // Fetch the html text of the website.
> > > string htmlText = GetPageHtml(urlString);
> > >
> > > // Search for the IP within that page.
> > > string regExpr = @".*>(\d+.\d+.\d+.\d+)</*";
> > > Regex rex = new Regex( regExpr, RegexOptions.None);
> > > if (rex.Match( htmlText).Success )
> > > {
> > > string ip = rex.Match( htmlText).Result("$1").Trim();
> > > Console.WriteLine(ip);
> > > }
> > >
> > > }
> > >
> > > //------------------------------------------------------
> > > // Get the HTML text of a give URL
> > > //------------------------------------------------------
> > > private static String GetPageHtml(string url)
> > > {
> > > StringBuilder sb = new StringBuilder();
> > >
> > > try
> > > {
> > >
> > > WebRequest myRequest = WebRequest.Create(url);
> > >
> > > // Return the response.
> > > WebResponse myHttpWebResponse = myRequest.GetResponse();
> > >
> > > // Gets the stream associated with the response.
> > > Stream receiveStream =
> > > myHttpWebResponse.GetResponseStream();
> > > Encoding encode =
> > > System.Text.Encoding.GetEncoding("utf-8");
> > >
> > > // Pipes the stream to a higher level stream reader with
> > > the required encoding format.
> > > StreamReader readStream = new StreamReader( receiveStream,
> > > encode );
> > >
> > > Char[] read = new Char[256];
> > >
> > > // Reads 256 characters at a time.
> > > int count = readStream.Read( read, 0, 256 );
> > > while (count > 0)
> > > {
> > > // Dumps the 256 characters on a string and displays
> > > the string to the console.
> > > String str = new String(read, 0, count);
> > > //Console.Write(str);
> > > sb.Append(str);
> > > count = readStream.Read(read, 0, 256);
> > > }
> > >
> > > // Releases the resources of the response.
> > > myHttpWebResponse.Close();
> > >
> > > // Releases the resources of the Stream.
> > > readStream.Close();
> > >
> > > }
> > > catch ( Exception )
> > > {
> > > return "";
> > > }
> > >
> > > String strHtml = sb.ToString();
> > > return strHtml.ToString();
> > > }
> > > }
> > > }
- Next message: Dmitry Karneyev: "Crystal Reports Viewer"
- Previous message: Dennis Myrén: "Re: switch or if?"
- In reply to: Anonieko Ramos: "Re: Here is another implementation using IPAddress and IPHostEntry classes"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|