Developer

Resolve host names with .NET

After talking with a developer who was oblivious to .NET, Tony Patton decided to write about basic network programming. He covers network namespaces, name resolution, and working with an IP address.

I was recently discussing programming with a developer who is oblivious to the .NET platform. He's in the middle of developing an application that utilizes network programming, and he quipped "try that with dot-not." I smirked as I proclaimed the ease at which I could accomplish the same task with C#, VB.NET, J#, and so forth. I'm never sure why developers think their platform is the best, because being a good programmer is beyond the platform you use—platforms change inevitably. Now let's talk basic network programming in .NET.

Network namespace

The .NET Framework class library provides network programming functionality in the System.Net and System.Net.Sockets namespaces. These allow you to work with everything from Internet connections to socket-based programming.

Here are several of the classes in the System.Net namespace:

  • IPAddress: Returns the IP address (xxx.xxx.xxx.xxx format) of a computer on an IP network.
  • DNS: Provides simple domain resolution functionality. If there are multiple entries in the DNS table, multiple instances are returned.
  • IPHostEntry: Microsoft Help defines the IPHostEntry class as a container for Internet host address information. It associates a DNS host name with an array of aliases and an array of matching IP addresses. It's a helper for the DNS class.

Name resolution

The DNS class is a static class that allows you to easily look up a specific host from the Internet Domain Name System (DNS). It requires Internet connectivity to properly function. The host information a DNS query retrieves is returned as an IPHostEntry object. If the specified host has more than one entry in the DNS database, the IPHostEntry object contains multiple IP addresses and aliases. You may use a for loop to access the entire address.

Here are three methods applicable to getting host information:

  • GetHostByName: A host is retrieved using the name passed into the method.
  • GetHostByAddress: A host is retrieved using the address passed into the method.
  • GetHostName: Returns the name assigned to a host.

A call to any of these DNS methods returns an instance of the IPHostEntry class. The following code creates an instance of the IPHostEntry class using the specified CNET host name:

IPHostEntry inetServer = Dns.Resolve("www.cnet.com");

Or, its VB.NET equivalent:

Dim inetServer As IPHostEntry
inetServer = Dns.Resolve(www.cnet.com)

With the IPHostEntry object populated with IP addresses, we can use them as IPAddress objects.

Working with an IP address

The IPAddress class makes it easy to work with IP network addresses. A new instance of the class is created easily by using an IP address as its constructor, but it's better to combine it with the System.Net's DNS class. Once the DNS class is used to populate an IPHostEntry object, it's populated with one or more IPHost objects containing the addresses resolved using the DNS object.

The following code listing (C# console application) accesses these objects:

Using System;
Using Sytem.Net;
namespace BuilderExamples {
class BuilderIPAddress {
static void Main(string[] args) {
try {
IPHostEntry iphe = Dns.Resolve("www.microsoft.com");
foreach (IPAddress addr in iphe.AddressList) {
Console.WriteLine("AddressFamily: " + addr.AddressFamily.ToString());
Console.WriteLine("Address: " + addr.ToString());
} } catch (Exception e) {
Console.WriteLine("Error: " + e.ToString());
} } } }

 

This example resolves Microsoft's Internet domain name and processes the list of IP addresses assigned to it. In addition, it returns the address family for each IP address. This has the possible values InterNetwork for the current IP version, and InterNetworkV6 for the next generation of IP called IPv6.


Output:

AddressFamily: InterNetwork
Address: 207.46.250.252

AddressFamily: InterNetwork
Address: 207.46.144.222

AddressFamily: InterNetwork
Address: 207.46.156.188

AddressFamily: InterNetwork
Address: 207.46.156.220

AddressFamily: InterNetwork
Address: 207.46.156.252

AddressFamily: InterNetwork
Address: 207.46.244.188

AddressFamily: InterNetwork
Address: 207.46.245.92

AddressFamily: InterNetwork
Address: 207.46.249.252

The VB.NET version follows:

Imports System
Imports System.Net
Module Module1
Sub Main()
Try
Dim iphe As IPHostEntry
Dim addr As IPAddress
iphe = Dns.Resolve("www.microsoft.com")
For Each addr In iphe.AddressList
Console.WriteLine("AddressFamily: " + _
addr.AddressFamily.ToString())
Console.WriteLine("Address: " + _
addr.ToString())
Next
Catch ex As Exception
Console.WriteLine("Error: " + ex.ToString())
End Try
End Sub
End Module

This approach may also be used to find the address of the current machine. This is achieved by using the GetHostName method of the Dns class. The next code listing includes a VB.NET console application that returns the IP address and machine name on which the code runs.

Imports System
Imports System.Net
Module Module1
Sub Main()
Dim i As Integer
Dim currentMachine As String
currentMachine = Dns.GetHostName()
Console.WriteLine("Host Name: " + currentMachine)
Dim iphe As IPHostEntry
iphe = Dns.GetHostByName(currentMachine)
Dim ipAddresses() As IPAddress = iphe.AddressList
For i = 0 To ipAddresses.GetUpperBound(0)
Console.Write("IP Addresses {0}: {1} ", i, ipAddresses(i).ToString)
Next i
End Sub
End Module

On my development machine (running on an internal network), it generates the following output:

Host Name: pentium4
IP Addresses 0: 192.168.1.101

The C# equivalent follows:

using System;
using System.Net;
namespace CSharpIPExample {
class Class1 {
static void Main(string[] args) {
string currentMachine;
IPHostEntry iphe;
currentMachine = Dns.GetHostName();
Console.WriteLine("Host Name: " + currentMachine);
iphe = Dns.GetHostByName(currentMachine);
IPAddress[] ipAddresses = iphe.AddressList;
for (int i = 0; i < ipAddresses.GetUpperBound(0); i++) {
Console.WriteLine("IP Addresses {0} : {1}", i, ipAddresses[i].ToString());
} } } }

Summary

The .NET Framework provides the System.Net and System.Net.Sockets namespaces for working with networking functions. The classes provided in these namespaces make it easy to integrate networking functionality into your application.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks