ConnectException:操作超时

时间:2011-03-25 16:53:05

标签: java

我有以下类打开套接字连接以查询WHOIS服务器。不幸的是,每次我运行它(例如java WHOIS google.com)时,它总是抛出异常:

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)

问题似乎是连接超时,我尝试将超时设置为更长的值(例如5000)并且它不起作用。它与代理和/或防火墙有关吗?我整天都陷入困境。

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);

    }

}

1 个答案:

答案 0 :(得分:2)

检查是否是防火墙问题的一种简单方法是使用telnet从同一台计算机连接到目标主机/端口。

例如,在我的Linux机器上:

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

如果您能够连接,那么问题在于您的Java程序。否则问题出在其他地方(防火墙等)。