Resolve host names with .NET
Takeaway: 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:
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:
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!
SponsoredWhite Papers, Webcasts, and Downloads
- IBM Multiform Master Data Management: The evolution of MDM applications IBM
- Demo: Need Disk Space? IBM DB2 9 Compression Demo IBM
- Finance Accounting Solutions Buyer's Guide Inside Business Finance
- Compare Top Hosted VoIP Phone Systems From Vendors like Packet8, Covad and Aptela VoIP-News
- Sprint DataLink for Wireless WAN Fact Sheet Sprint
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

