Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
i am using System.Net, for getting the ip of system the code is given below:

C#
public string getip()
 {
     IPHostEntry host;
     string localIP = "?";
     host = Dns.GetHostEntry(Dns.GetHostName());
     foreach (IPAddress ip in host.AddressList)
     {
         if (ip.AddressFamily.ToString() == "InterNetwork")
         {
             localIP = ip.ToString();
         }
     } return (localIP);
 }


i am getting the right ip when i am running my application on localhost but when i am hosting this application on server, it given the ip of hosted server.

please suggest me, how to get ip of client machine when application is deploy on server.

Please provide sample if possible
Thanks & Regards
Posted
Updated 11-Dec-11 22:21pm
v2

C#
public static string getclientIP()
{
    string result= string.Empty;
    string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
    {
        string[] ipRange = ip.Split(',');
        int le = ipRange.Length - 1;
        result = ipRange[0];
    }
    else
    {
        result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }
 
    return result;
}


If u run u will get the ip address 127.0.0.1.Here 127.0.0.1 is localhost. this means you are working on your PC. If you try other PC to use your app, you will get a different ip address.what i mean is host your application in your IIS or on your server and another pc must use your application. with that, you will get a different IP. 127.0.0.1 is localhost, meaning, current IP address.
 
Share this answer
 
v2
Comments
Ganesh Dutt Pandey 12-Dec-11 3:43am    
Hiusing this function ip= Unknown.
i want the user machine ip where my application is running.
[no name] 12-Dec-11 4:26am    
I think this is my code :)
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=577[^]



http://www.w3schools.com/asp/coll_servervariables.asp[^]

The following example demonstrates how to find out the visitor's browser type, IP address, and more:

XML
<html>
 <body>
 <p>
 <b>You are browsing this site with:</b>
 <%Response.Write(Request.ServerVariables("http_user_agent"))%>
 </p>
 <p>
 <b>Your IP address is:</b>
 <%Response.Write(Request.ServerVariables("remote_addr"))%>
 </p>
 <p>
 <b>The DNS lookup of the IP address is:</b>
 <%Response.Write(Request.ServerVariables("remote_host"))%>
 </p>
 <p>
 <b>The method used to call the page:</b>
 <%Response.Write(Request.ServerVariables("request_method"))%>
 </p>
 <p>
 <b>The server's domain name:</b>
 <%Response.Write(Request.ServerVariables("server_name"))%>
 </p>
 <p>
 <b>The server's port:</b>
 <%Response.Write(Request.ServerVariables("server_port"))%>
 </p>
 <p>
 <b>The server's software:</b>
 <%Response.Write(Request.ServerVariables("server_software"))%>
 </p>
 </body>
 </html>
 
Share this answer
 
v2
REMOTE_ADDR and HTTP_X_FORWARDED_FOR are the two server variables we are interested in in order to attempt to capture a users IP address.
as
object UserIPAddress = null;
UserIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if (string.IsNullOrEmpty(UserIPAddress)) {
	UserIPAddress = Request.ServerVariables("REMOTE_ADDR");


The reason we look for the HTTP_X_FORWARDED_FOR value 1st is because of proxy servers and things of that nature as many users are behind one. If that value is not there we just grab the REMOTE_ADDR. Even by doing this there are going to be times when we do not get an IP Address or what we get is not accurate.

Reference Link :-
Retrieve a Users IP Address
[^]
 
Share this answer
 
Comments
Ganesh Dutt Pandey 12-Dec-11 4:37am    
Hi RaviRanjanKr
nothing is get from this code, it is same as above solution 1.
please suggest something else.
public static string GetIPAddress()
{
	System.Web.HttpContext context = System.Web.HttpContext.Current;
	string sIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
	if (string.IsNullOrEmpty(sIPAddress)) {
		return context.Request.ServerVariables["REMOTE_ADDR"];
	} else {
		string[] ipArray = sIPAddress.Split(new Char[] { ',' });
		return ipArray[0];
	}
}
 
Share this answer
 
Comments
Ganesh Dutt Pandey 12-Dec-11 4:44am    
Hi DEar,
on localhost is provide the ip=127.0.0.1
but After host on server, its gives ip=unknown.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900