When it comes to networking, IP addresses are a must-have. While it may not be as critical from a C# perspective, we will look about the System as a result of this. There’s a lot of .NET namespace involved in here.
We may only needed the IP address of a local device. So check the article below to see how we can Get IP address of the system using C# or VB.NET.
Table of Contents
It’s important to know whose IP address we’re looking for in order to get the IP address we need.
Ipconfig is the command that can be used to find your IP address. To get the basic network information like host name, IP address, and gateway, you can run this command on your Command Prompt in administrator mode.
We have few System.Net classes to deal with this system IP address. Hence, first need to use the namespace System.Net.
The GetHostName() method can be used to obtain the machine name (or host name) from system. This is a part of the DNS class, as well. The IP address of the Host is now available.
The GetHostByName() method and the AddressList array are required for this. GetHostByName’s argument is “host name,” and so on.
The IP address of your client can be obtained by calling Request.UserHostAddress in ASP.NET.
We get multiple IP addresses if you use a Windows application to get your local IP address. The netid part of an IP address can be compared to any other IP address in order to find a specific IP address you know.
var addressListArray = Dns.GetHostAddresses(Dns.GetHostName()
if (addressListArray).Length > 0)
{
ipAddress = addressListArray[0].ToString();
}
Dns.GetHostEntry(Dns.GetHostName()).AddressList
.Where(ips => !ips.IsIPv6LinkLocal && !ips.IsIPv6Multicast && !ips.IsIPv6SiteLocal)
.First()
.ToString();
Sub Main(args As String())
Dim hosts() As systemIpAddresses = Dns.GetHostAddresses(Dns.GetHostName())
For Each ipAddress As systemIpAddresses In hosts
If ipAddress.AddressFamily = AddressFamily.InterNetwork Then
Console.WriteLine($"IP {ipAddress} is IPv4")
End If
Next
End Sub
Apart from the snippet explained above in the article for getting the system IP address, we can use the two HTTP header fields HTTP_X_FORWARDED_FOR and REMOTE_ADDR for obtaining the IP address of the client connected to internet.
HTTP_X_FORWARDED_FOR – gets the originating IP Address of the client
REMOTE_ADDR – gets IP address of the remote machine connected
var ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
Make a console application in C# and use the below procedure to get IP address using C#
static void Main(string[] args)
{
//find host name in C#
string myHost= Dns.GetHostName();
Console.WriteLine( myHost );
//write the system IP
Console.WriteLine(Dns.GetHostByName(myHost).AddressList[0].ToString());
Console.ReadKey();
}
In this post we covered on how to get system IP address using C# or VB.NET. Hope you found this post helpful. Post your feedback in the comments sections.
OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…
JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…
The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…
Some of the Key factors that need to consider while architecting or designing a software…
The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…
The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…