I. Ping Simulation Implementation
The isreachable method of inetaddress can be used to implement the Ping function. The parameter in the Ping function sets the timeout time and the returned result indicates whether the connection is successful.
Try ...{
Inetaddress address = inetaddress. getbyname ("192.168.0.113 ");
System. Out. println (address. isreachable (5000 ));
} Catch (unknownhostexception e )...{
E. printstacktrace ();
} Catch (ioexception e )...{
E. printstacktrace ();
}
2. Implement Telnet Simulation
Use the socket connect (socketaddress endpoint, int timeout) method to implement the Telnet function. if an exception is caught, Telnet fails.
Socket server = NULL;
Try ...{
Server = new socket ();
Inetsocketaddress address = new inetsocketaddress ("192.168.0.201", 8899 );
Server. Connect (addresses, 5000 );
} Catch (unknownhostexception e )...{
System. Out. println ("Telnet failed ");
} Catch (ioexception e )...{
System. Out. println ("Telnet failed ");
} Finally ...{
If (server! = NULL)
Try ...{
Server. Close ();
} Catch (ioexception e )...{
}
}