Whois.java Source code

Java tutorial

Introduction

Here is the source code for Whois.java

Source

/*
Chapter 3 - Simple Protocols
Java 2 Network Protocols Black Book
by Al Williams     
Paraglyph Press 2001
    
*/
import java.net.*;
import java.io.*;

public class Whois {
    public void whois(String query) throws IOException {
        whois(query, "whois.internic.net");
    }

    public String whois(String query, String server) throws IOException {
        Socket sock = new Socket(server, 43);
        int c = 0;
        String outstring = "";
        OutputStream os = sock.getOutputStream();
        InputStream is = sock.getInputStream();
        query += "\r\n";
        os.write(query.getBytes("iso8859_1"));
        try {
            while (c != -1) {
                c = is.read();
                if (c != -1)
                    outstring += (char) c;
            }
        } catch (IOException e) {
        }
        return outstring;
    }

    public static void main(String[] args) {
        String hostname = "";
        String ulist = "";
        if (args.length == 0) {
            System.out.println("usage: whois [%host] query");
            System.exit(1);
        }
        int argn = 0;
        hostname = "whois.networksolutions.com"; // default
        if (args.length > 1 && args[0].charAt(0) == '%') {
            hostname = args[argn].substring(1);
            argn++;
        }
        for (int i = argn; i < args.length; i++)
            ulist += args[i] + " ";
        Whois worker = new Whois();
        try {
            System.out.println(worker.whois(ulist.trim(), hostname));
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}