这是我从Java Complete Reference: 7th Edition
获得的一个程序。稍作修改:
import java.net.*;
import java.io.*;
public class WhoIs {
public static void main(String[] args) throws Exception{
Socket s = new Socket("internic.net",43);
OutputStream os = s.getOutputStream();
String domain = "facebook.com"+"\n";
byte[] stringToByte = domain.getBytes();
os.write(stringToByte);
InputStream is = s.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader buf = new BufferedReader(reader);
String temp;
while((temp=buf.readLine())!="\n"){
System.out.println(temp);
}
}
}
它只是联系internic.net
并获取有关Facebook的信息。但是,套接字将无法连接。我得到如下例外:
Exception in thread "main" java.net.ConnectException: Connection refused: 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 javacomplete.WhoIs.main(WhoIs.java:8)
所有内容与中的内容完全相同,只有读取数据。我使用了BufferedReader
,而本书使用InputStream
来读取单个字符。
帮助!为什么不连接?
答案 0 :(得分:4)
正确的主机名是whois.internic.net
而不是internic.net
:
$ telnet internic.net 43
Trying 192.0.43.9...
telnet: connect to address 192.0.43.9: Connection refused
Trying 2001:500:88:200::9...
telnet: connect to address 2001:500:88:200::9: No route to host
telnet: Unable to connect to remote host
$ telnet whois.internic.net 43
Trying 199.7.56.74...
Connected to whois.internic.net.
Escape character is '^]'.
答案 1 :(得分:1)
从“拒绝连接”错误中可以清楚地看到,internic.net
端口43上没有运行WHOIS服务器。
您想联系哪个WHOIS服务器?大多数域名注册商运行WHOIS服务器,许多ccTLD运营商也运行一个(例如whois.cira.ca
ccTLK ca
),所有RIR(ARIN,RIPE等)运行一个, gTLD注册管理机构等whois.internic.net
有一个等等......