Wednesday, November 11, 2015

Query InterNIC server's whois

Last example show Java example using WhoisClient class to query whois. Alternatively, we can query InterNIC server's whois without WhoisClient class.


package javawhois;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaWhoIs {
    
    private final static String WHO ="google.com";

    private final static String WHOIS_HOST = "whois.internic.net";
    private final static int WHOIS_PORT = 43;

    public static void main(String[] args) {
        int c;
        Socket socket = null; 

        String query = "=" + WHO + "\r\n";
        byte buf[] = query.getBytes();
        
        try {
            socket = new Socket(WHOIS_HOST, WHOIS_PORT);
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream(); 

            out.write(buf); 
            out.flush();
            while ((c = in.read()) != -1) { 
                System.out.print((char) c); 
            } 
            System.out.print("\nDone\n");
        } catch (IOException ex) {
            Logger.getLogger(JavaWhoIs.class.getName()).log(Level.SEVERE, null, ex);
            System.out.print(ex.getMessage());
        } finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException ex) {
                    Logger.getLogger(JavaWhoIs.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
}


No comments:

Post a Comment