using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace whois
{
class Program
{
public class Whois
{
private static int portNumber = 43;
private string host;
public Whois(string hostName)
{
this.host = hostName;
}
public void lookUp(string searchText)
{
TcpClient theConnection = null;
NetworkStream network = null;
BufferedStream basestream = null;
StreamReader inputstream = null;
StreamWriter outputstream = null;
try
{
theConnection = new TcpClient(host, portNumber);
network = theConnection.GetStream();
basestream = new BufferedStream(network);
}
catch (SocketException se)
{
Console.WriteLine("exception caught attempting to open connection to {0}", host);
Console.WriteLine("\nexception details: {0}", se.ToString());
return;
}
try
{
outputstream = new StreamWriter(basestream);
outputstream.WriteLine(searchText);
outputstream.Flush();
}
catch (Exception e)
{
Console.WriteLine("exception caught attempting to send data to host: {0}.", host);
Console.WriteLine("\nexception details:{0}", e.ToString());
theConnection.Close();
return;
}
try
{
inputstream = new StreamReader(basestream);
string intermediateoutput;
while (null != (intermediateoutput = inputstream.ReadLine()))
{
Console.WriteLine(intermediateoutput);
}
}
catch (Exception e)
{
Console.WriteLine("exception caught attempting to read data from host: {0}", host);
Console.WriteLine("\nexception details:{0}", e.ToString());
theConnection.Close();
return;
}
theConnection.Close();
}
}
static void Main(string[] args)
{
string host;
string search;
if (0 == args.Length)
{
Console.WriteLine("usage:whois<lookup host> hostname");
return;
}
else if (2 == args.Length)
{
host = args[0];
search = args[1];
}
else
{
host = "whois.internic.net";
search = args[0];
}
Whois whoisinstance = new Whois(host);
whoisinstance.lookUp(search);
}
}
}
No comments:
Post a Comment