gpt4 book ai didi

java - 连接异常 : operation timed out

转载 作者:行者123 更新时间:2023-12-02 08:21:11 24 4
gpt4 key购买 nike

我有以下类,它打开套接字连接来查询 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);

}

}

最佳答案

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

例如,在我的 Linux 机器上:

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

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

关于java - 连接异常 : operation timed out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5435564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com