473,378 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

IP Address Returning Question

110 100+
I am using a Ping member to ping a website from 2 different computers. The first computer is the websites server where all the files etc for the website are stored. The second computer running the test is a computer not hosting those files, so any computer not the server.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. string website = "www.somesite.com";
  2.             Ping ping = new Ping();
  3.             PingReply reply = ping.Send(website);
  4.             if (reply.Status == IPStatus.Success)
  5.             {
  6.                 rtxtbxDemo.Text = ("Address: " + reply.Address.ToString());
  7.             }
  8.  
The problem I am running into is that on the computer that is not the server the returned ip address is correct and reflects that websites dynamic IP address. When I run the app from the server it returns what I can only assume is the local IP address?
Request 1 (non server): 174.23.226.240
Request 2 (server): 127.0.0.2

I need the ip address that is returned by the non server computer however I need to run the program on the actual sever computer. What IP address am I most likely having returned when pinging the site on the server computer itself and how do I send a request for the websites IP like is being returned in request 1?
Nov 6 '10 #1
11 3460
Curtis Rutland
3,256 Expert 2GB
This is really a network type question. The server doesn't know it's own external IP address. That's something a router handles. The DNS resolution to the URL is local, so it pings the local address.

You'd need to find out the external address of this web server, and ping that IP rather than the URL itself.

If you were looking to programatically obtain a machine's IP address, you could use WhatIsMyIp.com's Automation page. Read more about that here.
Nov 6 '10 #2
Fuzz13
110 100+
So when I ping the site from the non sever computer it returns an IP address from that servers router, but when the server itself tries to ping the site it gathers its local IP not what is returned via the router. So in order to get the same returned IP address from the server I need to have something like WhatIsMyIp.com to send the request back at my server so it can tell me what it gets returned from the router as the IP address, correct?

When I use the Ping member it returns the Address in a very clean use-able variable. When I use the whatsmyip.com/automation I assume I will have to do some formatting to clean up any kind of returned data so I have just the needed IP address returned?
Nov 6 '10 #3
Curtis Rutland
3,256 Expert 2GB
That page should just respond with a string. No HTML, nothing except for the characters of your IP address. You could split them around '.' and make an IP out of it.
Nov 6 '10 #4
Fuzz13
110 100+
My last question on that is the program would need to ping that website "http://www.whatismyip.com/automation/n09230945.asp" which would tell the website to send its own request back at the requesting computers router which then would have the website display the returned IP address? So my code needs to instead send its ping request at the mentioned url rather then the website I would want to monitor from an outside computer?

ex. Outside computer sends ping request at: www.mywebsite.com returns ip address for that site.
Server computer sends ping request at: http://www.whatismyip.com/automation/n09230945.asp which then thru formatting would have a returned string of the ip address.

Does that sound accurate?
Nov 6 '10 #5
Fuzz13
110 100+
I am becoming confused why my ping is not correct though? When I send a ping from any computer it should go to that url and return that url's IP address right? Why would that be different on the server? The server should be sending pinging the url as well which would then return the IP address?
Nov 6 '10 #6
Plater
7,872 Expert 4TB
To answer that last question, you would need a better understanding of networking.

For using the automation page:
All you would do is populate an IPAddress object (do some from your server computer's code) with the page's result text and you will have your server's outside IPaddress. Try using HttpWebRequest. They should create the same IP as the Ping command you used on other computers.
You would NOT use the ping command on the whatsmyIP automation page.
Nov 8 '10 #7
Fuzz13
110 100+
I'm having some issues understanding how to return the string from whatsmyipaddress's automation page. I can't seem to figure out what method I call using the HttpWebResponse. I've got:

Expand|Select|Wrap|Line Numbers
  1.  HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com/automation/n09230945.asp");
  2.             HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
  3.  
  4.             rtxtbxDemo.Text = myHttpWebResponse.ToString();
  5.  
MSDN is not helping me very much on this as I can't understand what they are doing in their examples. What would I need to use to call the returned data from that site, meaning the string it lists?
Nov 14 '10 #8
Fuzz13
110 100+
I found a great little example that did exactly what I needed. This is the code:
Expand|Select|Wrap|Line Numbers
  1.  // used to build entire input
  2.             StringBuilder sb = new StringBuilder();
  3.  
  4.             // used on each read operation
  5.             byte[] buf = new byte[8192];
  6.  
  7.             // prepare the web page we will be asking for
  8.             HttpWebRequest request = (HttpWebRequest)
  9.                 WebRequest.Create("http://www.whatismyip.com/automation/n09230945.asp");
  10.  
  11.             // execute the request
  12.             HttpWebResponse response = (HttpWebResponse)
  13.                 request.GetResponse();
  14.  
  15.             // we will read data via the response stream
  16.             Stream resStream = response.GetResponseStream();
  17.  
  18.             string tempString = null;
  19.             int count = 0;
  20.  
  21.             do
  22.             {
  23.                 // fill the buffer with data
  24.                 count = resStream.Read(buf, 0, buf.Length);
  25.  
  26.                 // make sure we read some data
  27.                 if (count != 0)
  28.                 {
  29.                     // translate from bytes to ASCII text
  30.                     tempString = Encoding.ASCII.GetString(buf, 0, count);
  31.  
  32.                     // continue building the string
  33.                     sb.Append(tempString);
  34.                 }
  35.             }
  36.             while (count > 0); // any more data to read?
  37.  
  38.             // print out page source
  39.              rtxtbxDemo.Text = sb.ToString();
  40.  
I am populating the returned websites data into a richtext box here.

I think this will do what I need. I hope it helps someone else as well.
Nov 14 '10 #9
Plater
7,872 Expert 4TB
The manor in which the bytes from the response stream are read back is a bit messy, but yes, that is pretty much what we were suggesting.
Nov 15 '10 #10
Fuzz13
110 100+
Plater: I am entirely open to suggestions that cleans up the code. As I mentioned I got it from an example and I only know it returns what I need. I have been going thru it piece by piece as I do not understand it thoroughly.
Nov 15 '10 #11
Plater
7,872 Expert 4TB
Modified:
Expand|Select|Wrap|Line Numbers
  1. public static string GetIpAddress()
  2. {
  3.   string retval = "";
  4.   try
  5.   {
  6.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com/automation/n09230945.asp");
  7.         // execute the request 
  8.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  9.         StreamReader sr = new StreamReader(response.GetResponseStream());
  10.         retval=sr.ReadLine();
  11.   }
  12.   catch (Exception ee)
  13.   {//do failure logic here
  14.   }
  15.   return retval;
  16. }
  17.  
Then you can say:
rtxtbxDemo.Text =GetIpAddress();
Nov 15 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Geoff Pennington | last post by:
Is there an easy way to find out what classes implement a given interface? For example, it might be useful to know what classes implement ICollectible. Much obliged, Geoff.
7
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
5
by: Adrian | last post by:
I am trying to pass the address of a C++ function into a Fortran routine to enable the Fortran routine to call this C++ function. I have to do it this way as our build process does not allow...
5
by: Naveen Mukkelli | last post by:
Hi all, How can we use "NUnit" in socket programming. I mean, I'm writing a server program which accepts connection requests from the clients. I want to test the number of clients whenever a...
6
by: Al | last post by:
Hi I need to get the IP address of a computer on the network that uses dynamic IP addressing. Is there anyway I get the address but providing the name of the computer? Thanks in advance Al
0
by: BubbaThree | last post by:
I started having these problems Monday, I believe but I am not sure that our corporate office deployed some security patches??? First of all, when a go to my Web Application (written in ASP.NET...
3
by: Levi | last post by:
Hi, In my application I need the user to input an address. This application connects to multiple application via web services. Each application has its own unique business rules. One of the...
2
by: esource | last post by:
We have a db that is accessed by users on the web 24/7. Problem is when our maintenace job runs users are getting killed with this error: Database Error: SQL Error #-2147217900 Transaction manager...
14
by: subramanian100in | last post by:
Suppose fgets is used to read a line of input. char str; fgets(str, sizeof(str), stdin); After reading some characters on the same line, if end-of-file is encountered, will fgets return the 'str'...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.