Getting the Who-Is Information of a Hostname

This C# tutorial will teach you how retrieve who-is information for domain-names and IPs from who-is servers such as whois.internic.net.

How Who-Is Works The code that we’ll be writing in this tutorial can be used for a C# Windows application, a VB.NET Windows application and very easily an ASP.NET website. The main purpose of the tutorial is to ask the who-is servers about information on a specific hostname, be it a domain-name or IP address. Since information on various domain-names is provided by several who-is servers, we will query the appropriate who-is server. For instance if you want to retrieve information on an IP address, you will need to query whois.arin.net most of the time, however if you want information on a .com, .net or .edu domain-name, you’ll have to use whois.internic.net.

Designing the application Start a new C# Windows Application project in Visual Studio 2005 and throw on it 3 labels, 1 ComboBox (cmbServer), 1 TextBox (txtHostName), 1 Button (btnLookup) and finally 1 TextBox (txtResponse) that has the Multiline property set to True since that’s where the response from the server will be stored, which will typically be a long one. In the end your form should look similar to this:

In the cmbServer ComboBox you’ll want to put in a number of who-is servers. The attached project has the big three: whois.internic.net, whois.ripe.net and whois.arin.net – however you’ll find out that there are many other if you do a quick search on the web.

Coding the Who-Is The code we’re going to use is a simple communication between the client and the server. The client asks the server for information on the hostname, and then retrieves the response from the server. The connection then closes and the retrieval process is complete.

First of all you’ll want a default item in the cmbServer ComboBox selected when the form shows up:

private void Form1_Load(object sender, EventArgs e)

{

    // Has the first value selected

    cmbServer.SelectedIndex = 0;

}

Next are the appropriate using statements; since we’ll be using network connections and streams, we need these two:

using System.IO;

using System.Net.Sockets;

We’ll be using a TcpClient object to connect to the server, network streams for communicating between the server and the client, and stream readers and writers that will hook up to the network stream. Thus we’ll need the following objects declared inside the class:

// Networking and IO objects we'll be using throughout the application

TcpClient tcpWhois;

NetworkStream nsWhois;

BufferedStream bfWhois;

StreamWriter swSend;

StreamReader srReceive;

The rest of the code will now be inside the Click event of the btnLookup Button, although in real-life you’ll probably want to put it into a nice method for reusage purposes, in the spirit of object-oriented programming.

private void btnLookup_Click(object sender, EventArgs e)

{

    try

    {

        // The TcpClient should connect to the who-is server, on port 43 (default who-is)

        tcpWhois = new TcpClient(cmbServer.SelectedItem.ToString(), 43);

        // Set up the network stream

        nsWhois = tcpWhois.GetStream();

        // Hook up the buffered stream to the network stream

        bfWhois = new BufferedStream(nsWhois);

    }

    catch

    {

        MessageBox.Show("Could not open a connection to the Who-Is server.");

    }

    // Send to the server the hostname that we want to get information on

    swSend = new StreamWriter(bfWhois);

    swSend.WriteLine(txtHostName.Text);

    swSend.Flush();

    // Clear the textbox of anything existing content

    txtResponse.Clear();

    try

    {

        srReceive = new StreamReader(bfWhois);

        string strResponse;

        // Read the response line by line and put it in the textbox

        while ((strResponse = srReceive.ReadLine()) != null)

        {

            txtResponse.Text += strResponse + "\r\n";

        }

    }

    catch

    {

        MessageBox.Show("Could not read data from the Who-Is server.");

    }

    // We're done with the connection

    tcpWhois.Close();

}

As you can see here, nothing extraordinary takes place in order to retrieve the who-is information of a host, merely a connection through the TCP protocol to port 43 of a who-is server from where the data is read, just after it was sent the hostname that we have an interest in.

Here are two examples of the little application that we built, in action. In the first screenshot we’re looking at the who-is info of Geekpedia.com, and in the second at an IP that belongs to the Road Runner ISP. Note how we used different who-is servers, since whois.internic.net will not return information on IP addresses, just as whois.arin.net will not return any information on domain-names.

I hope you enjoyed this tutorial, but no matter if you did or didn’t, feel free to leave a comment.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top