-1

I'm receiving java socket programming exception. This is a code from the Book "Java Complete reference Oracle"

import java.net.*;
import java.io.*;

public class Whois {
public static void main(String[] args)throws Exception{
    int c;

    Socket s = new Socket("whois.internic.net",43);

    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();

    String str = (args.length == 0 ? "OraclePressBooks.com" : args[0]) + "\n";
    byte buf[] = str.getBytes();


    out.write(buf);


    while((c=in.read())!=-1)
    {
        System.out.println((char)c);
    }
    s.close();
}
}

I'm getting following exception. But Why?

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Whois.main(Whois.java:8)

P.S. I'm using Eclipse Photon. I tried running eclipse "as administrator" and also without it.

Nitish Prajapati
  • 139
  • 1
  • 11

1 Answers1

0

You do not have network connectivity to remote TCP port 43.

But since you've written that you have "proper network connection" because you are "using WiFi", we may suppose you have at least a web access (through a transparent proxy, or direct connections).

Therefore, you can simply use a Whois web service to access Whois databases. Some registrars offer a RWS-DNRD endpoint, that is a RESTful Web Service for Domain Name Registration Data (https://tools.ietf.org/id/draft-sheng-weirds-icann-rws-dnrd-01.html). You will find many examples of RESTful clients, for instance here: https://www.javacodegeeks.com/2012/09/simple-rest-client-in-java.html

In your case, you want to access the Internic database, so you can simply query their web form, using a GET request, like that (Java 9):

URL u = new URL("https://reports.internic.net/cgi/whois?whois_nic=OraclePressBooks.com&type=domain");
try (InputStream in = u.openStream()) {
    return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24