473,320 Members | 1,950 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,320 software developers and data experts.

How to get IP-Address of the client

Hi

i used all the following codes to access the IP Address of the client. it is showing the server details only. Please help me in this. I am working with VS2005 ASP.Net. I want to capture the client who is entering wrong data. This was handled in the code, but not able to get user's System Name.
/************************************************** *************************************/
Dim host As System.Net.IPHostEntry
Dim COMP_NAME() As String
host = System.Net.Dns.GetHostByName(Dns.GetHostName)
Response.Write("Remote host ip = " & host.AddressList(0).ToString)



Response.Write("Remote host name = " & Dns.GetHostName.ToString)
Response.Write("server variable Remotehost = " & Request.ServerVariables.Item("REMOTE_HOST"))

Response.Write("server variable localhost = " & Request.ServerVariables("LOCAL_ADDR")) 'host.HostName '
Response.Write("server variable httphost = " & Request.ServerVariables("HTTP_HOST"))

'COMP_NAME(0).ToString.ToUpper '
Response.Write("server variable windowsidentity= " & System.Security.Principal.WindowsIdentity.GetCurre nt().Name)
Response.Write("server variable machine name= " & Environment.MachineName)


Dim sIPAddress As String

sIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If sIPAddress = "" Then sIPAddress = Request.ServerVariables("REMOTE_ADDR")
Response.Write("sIPAddress = " + sIPAddress + " <br>")
Dim hostaddresses() As IPAddress
Dim strName As String
hostaddresses = Dns.GetHostByName(Dns.GetHostName).AddressList
strName = Dns.GetHostByName(Dns.GetHostName).HostName
Dim i As Integer
For i = 0 To hostaddresses.GetLength(0) - 1
Response.Write(hostaddresses(i).ToString & "<br>")
Next
Response.Write(strName)
/************************************************** ****************************************/
Jul 31 '07 #1
8 7205
Plater
7,872 Expert 4TB
You can easily access the IP address of someone accessing your website with:
Expand|Select|Wrap|Line Numbers
  1. string usersIP=Request.UserHostAddress;
  2.  
Doing a reverse lookup (getting a hostname from an IP address) is really not a worthwhile endevour for in-application proccessing for web applications. It would be much more effective to do in post processing, ie:
Logging the IP address (and other info if desired) to a file and then afterwards have a program that checks for a reverse lookup.
Jul 31 '07 #2
You can easily access the IP address of someone accessing your website with:
Expand|Select|Wrap|Line Numbers
  1. string usersIP=Request.UserHostAddress;
  2.  
Doing a reverse lookup (getting a hostname from an IP address) is really not a worthwhile endevour for in-application proccessing for web applications. It would be much more effective to do in post processing, ie:
Logging the IP address (and other info if desired) to a file and then afterwards have a program that checks for a reverse lookup.

Hi

Thanks for your reply.

I used the one you suggested, but still it is returning 127.0.0.1 for UserHostAddress and UserHostName . Is there any settings need to be updated in IIS or any thing else.
Jul 31 '07 #3
nateraaaa
663 Expert 512MB
You can easily access the IP address of someone accessing your website with:
Expand|Select|Wrap|Line Numbers
  1. string usersIP=Request.UserHostAddress;
  2.  
Doing a reverse lookup (getting a hostname from an IP address) is really not a worthwhile endevour for in-application proccessing for web applications. It would be much more effective to do in post processing, ie:
Logging the IP address (and other info if desired) to a file and then afterwards have a program that checks for a reverse lookup.
I recently experienced a problem accessing the IP address because of a load balancer. Request.UserHostAddress would return the IP address of the load balancer instead of the IP of the machine accessing my application. Do you know of a way around this?

Nathan
Jul 31 '07 #4
Plater
7,872 Expert 4TB
Hi

Thanks for your reply.

I used the one you suggested, but still it is returning 127.0.0.1 for UserHostAddress and UserHostName . Is there any settings need to be updated in IIS or any thing else.

hehe are you testing your website from the same computer it's running on? because then it will of course be the localhost address. Get someone on another PC (or even on a different network)

For my situation:
There are 10 PCs on my network.
We all get 192.168.1.x IP addresses.
My IP is 192.168.1.10
My router/nat is 192.168.1.1
I have a server on 192.168.1.100.
I have it accessable to the public through say: 24.123.44.67
While on my computer:
If I go to http://192.168.1.100, the log shows that "192.168.1.10 accessed website"
If I go to http://24.123.44.67 the log shows that "192.168.1.1 accessed website"
The router is smart enough to know that I am going right back inside my network so it never goes out.

If I am at home and my ip address is 67.228.12.13 and I got to http://24.123.44.67 the log shows that "67.228.12.13 accessed website"
Jul 31 '07 #5
nateraaaa
663 Expert 512MB
Hi

Thanks for your reply.

I used the one you suggested, but still it is returning 127.0.0.1 for UserHostAddress and UserHostName . Is there any settings need to be updated in IIS or any thing else.
Are you testing locally and getting the 127.0.0.1 everytime? Try this code locally

Expand|Select|Wrap|Line Numbers
  1. //works for local machine
  2. string strHostName = Dns.GetHostName();
  3. Response.Write("Host Name: " + strHostName + "<br>");
  4.  
  5. IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
  6.  
  7. int IP = 0;
  8. foreach(IPAddress ipaddress in iphostentry.AddressList)
  9. {
  10. Response.Write("IP #" + ++IP + ": " + ipaddress.ToString());
  11. }
However this will not work once moved to production. You will need to use Request.UserHostAddress as Plater suggested when you move this application into production.

Nathan
Jul 31 '07 #6
Thanks ya, i got it with the following approach
Dim host As System.Net.IPHostEntry
'host = System.Net.Dns.GetHostByAddress(Request.ServerVari ables.Item("REMOTE_HOST"))
host = System.Net.Dns.GetHostEntry(Request.ServerVariable s.Item("REMOTE_HOST"))
Dim strComputerName As String = host.HostName
Aug 1 '07 #7
Thanks ya, i got it with the following approach
Dim host As System.Net.IPHostEntry
'host = System.Net.Dns.GetHostByAddress(Request.ServerVari ables.Item("REMOTE_HOST"))
host = System.Net.Dns.GetHostEntry(Request.ServerVariable s.Item("REMOTE_HOST"))
Dim strComputerName As String = host.HostName
Here's an FYI.

A possible gotcha. If you have a router that pushes outside internet traffic to a web server, it's possible that router may be replacing the REMOTE_ADDR header with a hard-coded IP address.

For an example, download the Web Interface 4.0 troubleshooting guide from http://support.citrix.com/article/CTX106974 and refer to the code on page 43.
Aug 9 '07 #8
I think in web config file u mention this
string usersIP=Request.UserHostAddress;
Aug 11 '07 #9

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

Similar topics

3
by: Leszek | last post by:
Hi everybody, I need your help-I want to display all users which are online on my web page and I don't know how to do this. I know how to do this whit one IP but for more - I don't. HELP ME...
3
by: StinkFinger | last post by:
All, There are certain scripts that I have that only I want to run, both from home and sometimes work. If I add something like this (below) to the scripts, will this keep out unauthorized use (if...
1
by: StinkFinger | last post by:
Hello all, I am trying to update a function of mine to detect for private ip ranges. Here is what I have so far, and of course, I am stuck at the part where i need to compare the $ip to the...
8
by: UndoMiel | last post by:
Hi, I am looking for a way to validate IP addresses using XML Schemas. The following is what i used: <xsd:simpleType name="IPType"> <xsd:restriction base="xsd:string"> <xsd:pattern...
1
by: rlc | last post by:
I am using a toolkit, part of which creates TCP/IP connections using a function 'Connect(<local IP>,<local Port>,<Remote IP>,<Remote Port>)'. This function's signiture looks very similar to the...
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
2
by: nachotico | last post by:
hi i'm new using PHP here is what i need> i have a MySQL database that contain Decimal IPs -- the issue is that i don't know how to after extract them convert them to a real IP. here is what i...
3
by: Orendavd | last post by:
hi there, I have a working UDP server in c++, how to I print the IP of the received packet???
1
by: DEFFiCE | last post by:
Bonjour à tous ! Je suis à la recherche d'un script si il existe déjà ou de la manière à m'y prendre si il n'existe pas. Je vous explique j'aimerais réaliser où avoir un script qui permet de...
1
by: ScottZ | last post by:
With python 2.6 and wxpython I'm trying to create a system tray icon application based around an example found here:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.