一.模拟ping的實作
利用inetaddress的isreachable方法可以實作ping的功能,裡面參數設定逾時時間,傳回結果表示是否連上。 try { inetaddress address = inetaddress.getbyname("192.168.0.113"); system.out.println(address.isreachable(5000)); } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { }
二.模拟telnet的實作
利用socket的connect(socketaddress endpoint, int timeout)方法可以實作telnet的功能,如果catch到異常說明telnet失敗 socket server = new socket(); inetsocketaddress address = new inetsocketaddress("192.168.0.113", 8080); server.connect(address, 5000); server.close(); system.out.println("telnet失敗");
<a href="http://blog.csdn.net/hbcui1984/archive/2007/10/23/1839096.aspx" target="_blank">檢視本文來源</a>
歡迎加群互相學習,共同進步。qq群:ios: 58099570 | android: 330987132 | go:217696290 | python:336880185 | 做人要厚道,轉載請注明出處!http://www.cnblogs.com/sunshine-anycall/archive/2009/06/09/1499896.html