Interview Questions

What is Whois Client?

An Introduction to Socket Programming

(Continued from previous question...)

What is Whois Client?

Whois Client: This is whois, a very simple and generic whois client. This client, unlike the classic whois client, does not check for supported flags at the client side, except for -h (whois host) and -p (whois port).

The whois(1) client makes a TCP/IP connection 
to the server and conducts the kind of protocol
that you would type if you where to make 
a connection by hand:

    [7:30am julian] whois reggers
    There were 1 matches on your request.

               Full Name: Quinton, Reg
              Department: Info Tech Svcs
                    Room: NSC 214
                   Phone: 679-2111x(6026)
               Index Key: 481800
         Machine Address:
     Directory Addresses:

    For more information try 'whois help'.

The client sends the command "reggers", the server sends back the answer and the client displays the answer received to the user. When the server is finished the connection is closed.

Sample code: whois(1) client

sub tcpopen {
 use Socket;                # need socket interface
 my($server, $service) = @_;# args to this function
 my($proto, $port, $iaddr); # local variables
  # localized obscure handle

die("550:Cannot getprotobyname('tcp')\r\n")
       unless ($proto = getprotobyname('tcp'));

die("550:Cannot getservbyname($service)\r\n")
unless ($port = getservbyname($service, 'tcp'));

die("550:Cannot gethostbyname($server)\r\n")
      unless ($iaddr = gethostbyname($server));

die("550:Cannot create socket\r\n")
unless socket($handle, PF_INET, SOCK_STREAM, $proto);

die("550:Cannot connect($service://$server)\r\n")
unless connect($handle, sockaddr_in($port, $iaddr));

       # unbuffered I/O to that service

 select($handle); $| = 1; select(STDOUT); $| = 1;


(Continued on next question...)

Other Interview Questions