Introduction
'WHO-is' is a Network Tool or a Query. In general, it comes in practice to get the hidden details about the Host like Domain Name, IP Address, and details about the person/organization who is hosting.
User-Interface design
- Combo Box
- Text Box
- Button
- Label
- Progress Bar
Note. Put your Combo Box, Text Box, and Button inside a Group Box. Next, put a Multi-Line Text Box just aside from that group box. , rests are the Labels and Progress Bar which are not that tough to drag-drop from the toolbox.
After, your design effort you may get a result similar to the following window.
Background details
1. Combo box (cmb Server)
cmbServer contains three items you may add using c# code or by its property window. Name it as cmbServer. Right now, I prefer Property Window
Click on 'Items' and add three values in individual lines.
- Whois.internic.net [Default]
- Whois.ripe.net
- Whois.arin.net
2. Text box (txt HostName)
Drag a text box and put it in its corresponding location. It is as easy, as you have done earlier. Name it as txtHostName.From, here we get the hostname. Maybe, http://google.com, http://c-sharpcorner.com, or anything.
3. Button (btn Lookup)
Put a button on the form and name it btnLookUp and the Text must be Look Up or whatever you wish.
4. Multi-line text box (text response)
In this TextBox we will store the fetched data from the whois server. That's why, it must be multi-line with Horizontal Bar. Name this control as txtxResponse.
Now, there is a need to create the text box as a multi-line. So, click on the little arrow in the right corner of the text box. And, check the MultiLine Text Box.
5. Progress bar (progressbar1)
Put it just below the text box; the rest labels and the picture box are left for you.
C# Code
First, add some namespace
using System.IO;
using System.Net.Sockets;
Then create some Reference variables in the Form class
TcpClient tcpWhois;
NetworkStream nsWhois;
BufferedStream bfWhois;
StreamWriter strmSend;
StreamReader strmRecive;
In Form() Constructor, Define Progress Bar values
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
In Form_Load() event
Double-click on your active form
Here, we define the default index of Combo Box
cmbServer.SelectedIndex = 0;
Now, we will define some actual codes.
In the Lookup Button, Click Event has
tcpWhois = new TcpClient(cmbServer.SelectedItem.ToString(), 43);
// We assigned WHOis Server(from comboBox) and its corresponding Port(43) to the TcpClient Constructor.
Next,
nsWhois = tcpWhois.GetStream(); // Setup the Stream of Tcp client using GetStream() method
bfWhois = new BufferedStream(nsWhois); // Initializing the Buffer
strmSend = new StreamWriter(bfWhois);
strmSend.WriteLine(txtHostName.Text);
//Sending the Server & Host Name using StreamSend class
strmSend.Flush(); // Clear the buffer
txtxResponse.Clear(); // clear the text box
try
{
// Turn to Recive the data from Server
strmRecive = new StreamReader(bfWhois);
string response;
// variable, which 'll store the response from server.
//Continue the LOOP till it comes to the end (null)
while ((response = strmRecive.ReadLine()) != null)
{
//at every response append to New line using "\r\n"
txtxResponse.Text += response + "\r\n";
//increase the value of Progress Bar :D
if (progressBar1.Value < 100)
progressBar1.Value += 10;
}
}
catch
{
//FOUND any exception
MessageBox.Show("WHOis Server Error :x", "Error");
}
catch
{
MessageBox.Show("No Internet Connection or Any other Fault", "Error");
}
//SEND THE WHO_IS SERVER ABOUT THE HOSTNAME
finally
{
try
{
// Close the stream
tcpWhois.Close();
}
catch
{
//DO nothing
}
}
At End
Ultimately we have a working window that takes the Host Name as Input and shows the Whois Details about the Host.