How do I query a Whois server using a socket?
Author: Deron Eriksson
Description: This Java tutorial describes how to query a Whois server via a Socket.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


Page:    1 2 >

WhoisW servers typically run on port 43. We can query a Whois server by making a socket connection to a Whois server host and port. Since a whoisW server takes text data and returns text data, we can use a Writer to send data to the server and a Reader to accept data from the server.

The WhoisQuery class demonstrates this. It performs a Whois query for "abcnews.com" on two different Whois servers, "whois.enom.com" and "whois.internet.net". It creates a socket connection to the Whois server in question. It writes to the socket via a PrintWriter that writes to the socket's output stream. It reads from the socket via a BufferedReader that reads from the InputStreamReader that takes its input stream from the socket. The BufferedReader lets us read the returned results line-by-line. As we read each line, we display the results to standard output.

WhoisQuery.java

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class WhoisQuery {

	public static void main(String[] args) throws Exception {
		String domainNameToCheck = "abcnews.com";
		performWhoisQuery("whois.enom.com", 43, domainNameToCheck);
		performWhoisQuery("whois.internic.net", 43, domainNameToCheck);
	}

	public static void performWhoisQuery(String host, int port, String query) throws Exception {
		System.out.println("**** Performing whois query for '" + query + "' at " + host + ":" + port);

		Socket socket = new Socket(host, port);

		InputStreamReader isr = new InputStreamReader(socket.getInputStream());
		BufferedReader in = new BufferedReader(isr);

		PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
		out.println(query);

		String line = "";
		while ((line = in.readLine()) != null) {
			System.out.println(line);
		}
	}

}

(Continued on page 2)

Page:    1 2 >