开发者

ConnectException: operation timed out

I have the following class that opens a socket connection to query a WHOIS server. Unfortunately, every time I run it (e.g. java WHOIS google.com) it always throws the exception:

Contacting the WHOIS server for 'google.com' at whois.internic.net:43
Exception in thread "main" java.net.ConnectException: Operation timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)
    at WHOIS.performWhoisQuery(WHOIS.java:11)
    at WHOIS.main(WHOIS.java:28)

The problem seems connection timeout and i tried setting the timeout to a longer value (e.g. 5000) and it didn't work. Does it have to do with proxy and/or firewall? I got stuck whole day on it.

public class WHOIS {
    public static void performWhoisQuery(String host, int port, String query) throws Exception {
        System.out.println(" Contacting the WHOIS server for '" + query + "' at " + host + ":" + port);

        Socket socket = new Socket(host, port);

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

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

        String 开发者_高级运维aLine = "";
        while ((aLine = br.readLine()) != null) {
            System.out.println(aLine);
        }
    }

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

    }

}


One easy way to check whether it's a firewall issue is by using telnet to connect to the target host/port from the same computer.

For example, on my Linux box:

aix@aix:~$ telnet whois.internic.net 43
Trying 199.7.57.74...
Connected to whois.internic.net.
Escape character is '^]'.

If you're able to connect, then the problem is with your Java program. Otherwise the problem is somewhere else (firewall etc).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜