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