Tech Talk A software developer on all things tech, and then some


Getting the Client’s IP with C#

I needed a pretty reliable and easy way to get the client's IP address in one of the applications I'm working on. Granted the IP is just used for log purposes, I still wanted to pull from the request vs using some JS. There are multiple ways to do this, some work in some situations, some work in others, etc... So I decided to write a method that would work through the most reliable, then work my way down.

private string GetClientIPAddress()
            if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                // Let's first check for a proxy
                var ipAddresses = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',');
                return ipAddresses[0].Trim();
            else if (System.Web.HttpContext.Current.Request.UserHostAddress != null
                && System.Web.HttpContext.Current.Request.UserHostAddress != string.Empty)
                // If they are not using one
                return System.Web.HttpContext.Current.Request.UserHostAddress;
                // And the hail mary jic
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

While this won't be the answer 100% of the time, it should be good for most. Hope this helps and happy coding!

The code was made with help from the folling sources :

Tagged as: , 2 Comments

Introducing : FindIP

A new download is now available on the downloads page, FindIP. Find Ip is a simple tool to either return the domain name from a given IP or vice versa, or ping a given IP or domain. This quick project was spawned from me being tired of using convoluted tools that I found across the net, and not wanting to open up the command prompt and type all the commands out all the time. Really simple and small, but has been extremely useful so far. If you have any suggestions, feel free to contact me! Note : .NET Framework 4, WPF

How to Use

Finding a Domain Name

To find a domain name from a given IP, go the Domain tab, and enter either the single IP, or multiple IPs separated with a either a comma or space.

Finding an IP Address

To find an IP address from a given Domain, go to the IP tab, and enter either the single Domain Name, or multiple Domain Names separated with a either a comma or space.

Pinging an Address

To ping an IP or Domain name, go to the Ping tab and enter either an IP address or Domain Name

Again, if you have any suggestions or comments, please feel free to let me know below, or contact me! To download, please go to the downloads page.

Tagged as: , , No Comments

Checking Status of IP with Port Number in C#

Ever need to check the status of a server, but through a port as well? I needed to as well recently, and came up with a quick, clean method to check the status of a server address and port. This will return a boolean, giving the status of whether the server is running and port is open.

public static bool PingHost(string _HostURI, int _PortNumber)
		TcpClient client = new TcpClient(_HostURI, _PortNumber);
		return true;
	catch (Exception ex)
		MessageBox.Show("Error pinging host:'" + _HostURI + ":" + _PortNumber.ToString() + "'");
		return false;

Hope this helps and happy coding!

%d bloggers like this: