我正在尝试制作一个 telnet ip 端口的脚本,以查看服务器是启动还是关闭。(就像你在窗口命令提示符下所做的那样。cmd [enter] => telnet 11.111.11.11 200 (ip& port) 如果连接成功,程序将返回 true 否则返回 false。我需要这段代码才能真正有效,因为这函数将进入forloop,在这里我为每个ip在网站上进行所有显示。提前Thnx
PS Ops 我的意思是 java/jsp 我不好哈哈
try {
InetAddress addr = InetAddress.getByName("java.sun.com");
int port = 80;
SocketAddress sockaddr = new InetSocketAddress(addr, port);
// Create an unbound socket
Socket sock = new Socket();
// This method will block no more than timeoutMs.
// If the timeout occurs, SocketTimeoutException is thrown.
int timeoutMs = 2000; // 2 seconds
sock.connect(sockaddr, timeoutMs);
} catch (UnknownHostException e) {
} catch (SocketTimeoutException e) {
} catch (IOException e) {
}