/** * WhoisServer by James Powell - September 2000 * Based on ThreadedEchoServer by Cay Horstmann / Core Java Vol II */ import java.io.*; import java.net.*; public class WhoisServer { public static void main(String[] args) { int i = 1; try { ServerSocket s = new ServerSocket(8189); for (;;) { Socket incoming = s.accept(); System.out.println("Spawning " + i); new ThreadedWhoisHandler(incoming, i).start(); i++; } } catch (Exception e) { System.out.println(e); } } } class ThreadedWhoisHandler extends Thread { public final static String[] whoisHost = { "whois.crsnic.net", "whois.crsnic.net", "whois.crsnic.net", "whois.nic.uk", "whois.nic.uk" }; public final static String[] whoisTLD = { "com", "net", "org", "co.uk", "org.uk" }; public final static String[] whoisNoMatch = { "NO MATCH", "NO MATCH", "NO MATCH", "No match", "No match" }; public ThreadedWhoisHandler(Socket i, int c) { incoming = i; counter = c; } public void run() { try { BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */); WhoisLookup queryresults[] = new WhoisLookup[whoisTLD.length]; WhoisReturns resultscount = new WhoisReturns(); String str = in.readLine(); System.out.println("Searching for " + str + "\r\n"); for (int i = 0; i < whoisTLD.length; i++) { queryresults[i] = new WhoisLookup(str + "." + whoisTLD[i], whoisHost[i], 43, resultscount); queryresults[i].start(); } while (resultscount.getNumReturned() < whoisTLD.length) { Thread.sleep(100); // give the threads a chance to return } for (int i = 0; i < whoisTLD.length; i++) { if (queryresults[i].getReply().length() == 0) { out.println(str + "." + whoisTLD[i] + " not returned!"); } else if ((queryresults[i].getReply().indexOf(whoisNoMatch[i]) == -1)) { out.println(str + "." + whoisTLD[i] + " is not available"); } else { out.println(str + "." + whoisTLD[i] + " is available!"); } } incoming.close(); } catch (Exception e) { System.out.println(e); } } private Socket incoming; private int counter; } class WhoisLookup extends Thread { private String domain = ""; private String whoisHost = ""; private int whoisPort = 0; private WhoisReturns resultscount; private String results = ""; public WhoisLookup(String d, String wh, int wp, WhoisReturns rc) { domain = d; whoisHost = wh; whoisPort = wp; resultscount = rc; } public String getReply() { return results; } public void run() { Socket theSocket; DataInputStream theWhoisStream; PrintStream ps; try { theSocket = new Socket(whoisHost, whoisPort); ps = new PrintStream(theSocket.getOutputStream()); ps.print(domain + " \r\n"); theWhoisStream = new DataInputStream(theSocket.getInputStream()); byte [] theByte = new byte[1]; while (theWhoisStream.read(theByte) != -1) { Character c = new Character((char) theByte[0]); results += c.toString(); } } catch (IOException e) { System.err.println(e); } resultscount.incNumReturned(); } } // WhoisReturns - small utility class to assist locking of answer reports class WhoisReturns { private int numreturned = 0; final Object resource1 = "resource1"; // something to lock public WhoisReturns() {} public int getNumReturned() { return numreturned; } public void incNumReturned() { synchronized(resource1) { numreturned++; } } }