Monday, March 8, 2010

C# : HostName, MachineName and IPAddresses

Host Name and Machine Name are not the same. A Host Name is a name given to a host(e.g. computer or phone) by the Domain Name System(DNS). Therefore, this name is in context with the DNS. For more details have a look here :

On the other hand, a Machine Name is more in the local context. which is established during system start up and when the name is ready from the registry.

Code to find the Host Name :

String HostName = System.Net.Dns.GetHostName();

Code to find the Machine Name :
String MachineName = System.Environment.MachineName;

Code to find the IPAddress (IPv4) of a host :

System.Net.Dns.GetHostName() and "" (Empty String) refer to the same host name.
"localhost" refers to the reserved IP addresses in IPv4, or ::1 in IPv6.
For more details about localhost, take a look here :

System.Net.IPAddress IpAddress = System.Net.Dns.GetHostAddresses("").Single(ipAddressFamily => ipAddressFamily.AddressFamily == AddressFamily.InterNetwork);

is the same as

System.Net.IPAddress IpAddress = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).Single(ipAddressFamily => ipAddressFamily.AddressFamily == AddressFamily.InterNetwork);

but is NOT the same as

System.Net.IPAddress IpAddress = System.Net.Dns.GetHostAddresses("localhost").Single(ipAddressFamily => ipAddressFamily.AddressFamily == AddressFamily.InterNetwork);

.Single(ipAddress => ipAddress.AddressFamily == AddressFamily.InterNetwork) is a function which takes a Predicate Delegate as an argument. The predicate determines whether the object to which it is applied to meets the criteria stated in the Predicate. The predicate here is a lambda function/inline function. For e.g. here the predicate checks whether each ipAddress(from AddressList which is returned by System.Net.Dns.GetHostAddress(...)) satisfies the fact that the ipAddress.AddressFamily == AddressFamily.InterNetwork (IPv4). Since we are using "Single" here it means that, it will return only the one which satisfied the condition, if there are more than one which satisfy the condition, it will throw an error.

Look here for more of different delegates offered by C#:

Look here for more on lambda expressions :

Look here for more on delegates :

More on the power of Delegates later ...