1

I try to get IP-address and port of client which sends HTTP requests to the server. I used properties of HttpListenerRequest class. UserHostAddress property returns ip and port of server but not client. RemoteEndPoint returns some ip and port but it is not true client ip (I know that client requests are coming from port 1234) and every time it returns different data (I don't know why). How can I solve this? May be I should set http header From on the client side?

Thank you very much!

3
  • 1
    saw this ? stackoverflow.com/questions/735350/… Commented Jan 3, 2015 at 1:26
  • How do you know it's the wrong IP? You will likely get their outside facing IP, and depending on the network set-up it too may very. IP only really designates 'network'. What is behind that ip/network can be other networks or computers or evening cell phones Commented Jan 3, 2015 at 1:29
  • terary, I tested my application at the same machine (client and server are running at the same machine). And client port number and port received on server are not the same. Commented Jan 3, 2015 at 1:34

1 Answer 1

4

based on SO answer, I think this would help you...

 protected void GetUser_IP() { string VisitorsIPAddr = string.Empty; if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) { VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); } else if (HttpContext.Current.Request.UserHostAddress.Length != 0) { VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress; } uip.Text = "Your IP is: " + VisitorsIPAddr; } 
Sign up to request clarification or add additional context in comments.

1 Comment

N e w B e e, thank you. I saw this thread. But how can I receive port number?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.