java版的.检测主机在线,没有多线程,主要是学习这个函数.
代码:
/**
* @(#)GreenPing.java
*
*
* @author
* @version 1.00 2007/10/27
*/
import java.net.*;//java net class
import java.io.*;
public class GreenPing {
/**
* Creates a new instance of <code>GreenPing</code>.
*/
public GreenPing() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
int count=0;
System.out.println("正在检测,请等待:");
for(int i=1;i<244;i++)
{
String T="10.16.20."+i;
System.out.print('=');
InetAddress address = InetAddress.getByName(T);
if(address.isReachable(1000))//1000 ms
{
count++;
System.out.print(" ");
System.out.println("IP地址: "+T+" 主机名: "+address.getHostName());
}
}
System.out.println(" 共发现主机:"+count);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
* @(#)GreenPing.java
*
*
* @author
* @version 1.00 2007/10/27
*/
import java.net.*;//java net class
import java.io.*;
public class GreenPing {
/**
* Creates a new instance of <code>GreenPing</code>.
*/
public GreenPing() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
int count=0;
System.out.println("正在检测,请等待:");
for(int i=1;i<244;i++)
{
String T="10.16.20."+i;
System.out.print('=');
InetAddress address = InetAddress.getByName(T);
if(address.isReachable(1000))//1000 ms
{
count++;
System.out.print(" ");
System.out.println("IP地址: "+T+" 主机名: "+address.getHostName());
}
}
System.out.println(" 共发现主机:"+count);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
类:java.net.InetAddress
方法:isReachable()和getHostName();
没有多线程,会很慢的.255s
enjoy it!