Java--ping & Telnet Implementation

Source: Internet
Author: User
Tags getmessage

Ping & Telnet Implementation classes:

ImportOrg.springframework.data.web.JsonPath;Importjava.io.IOException;Importjava.net.*; Public classPtutil {/*** * Ping operation *@paramhostname *@paramTimeout in milliseconds *@return     */     Public Staticjsonresult pingresult (String hostname,integer timeout) {Jsonresult Jsonresult=NewJsonresult (); Try{inetaddress Address=Inetaddress.getbyname (hostname); BooleanFlag =address.isreachable (timeout); if(flag) {Jsonresult.setmessage ("Ping Result: the address is reachable."); }Else{jsonresult.setcode (Constants.ResultCode.EXCEPTION); Jsonresult.setmessage ("Ping Result: the address is unreachable."); }        } Catch(unknownhostexception e) {jsonresult.setcode (Constants.ResultCode.EXCEPTION); Jsonresult.setmessage ("Ping Result: unknownhostexception:" +e.getmessage ()); } Catch(IOException e) {jsonresult.setcode (Constants.ResultCode.EXCEPTION); Jsonresult.setmessage ("Ping Result: IOException:" +e.getmessage ()); }        returnJsonresult; }    /*** * telnet operation *@paramhostname *@paramTimeout in milliseconds *@return     */     Public Staticjsonresult telnetresult (String hostname,integer port,integer timeout) {Jsonresult Jsonresult=NewJsonresult (); Try{Socket server=NewSocket (); Inetsocketaddress Address=Newinetsocketaddress (Hostname,port);            Server.connect (address, timeout);            Server.close (); Jsonresult.setmessage ("Telnet Result: success!"); }Catch(unknownhostexception e) {jsonresult.setcode (Constants.ResultCode.EXCEPTION); Jsonresult.setmessage ("Telnet result: unknownhostexception:" +e.getmessage ()); } Catch(IOException e) {jsonresult.setcode (Constants.ResultCode.EXCEPTION); Jsonresult.setmessage ("Telnet result: IOException:" +e.getmessage ()); }        returnJsonresult; } }
View Code

Related classes:

 Public classJsonresult {PrivateString Code;//result Encoding    PrivateString message;//Result Description    PrivateObject data;  PublicJsonresult () { This. Setcode (Constants.ResultCode.SUCCESS);  This. Setmessage (Constants.ResultCode.SUCCESS.msg ()); }         PublicJsonresult (Constants.resultcode code) { This. Setcode (code);  This. Setmessage (Code.msg ()); }         PublicJsonresult (constants.resultcode code, String message) { This. Setcode (code);  This. Setmessage (message); }         PublicJsonresult (constants.resultcode code, String message, Object data) { This. Setcode (code);  This. Setmessage (message);  This. SetData (data); }         PublicString GetCode () {returnCode; }     Public voidSetcode (Constants.resultcode code) { This. Code =Code.val ();  This. Message =code.msg (); }     PublicString getMessage () {returnmessage; }     Public voidsetmessage (String message) { This. Message =message; }     PublicObject GetData () {returndata; }     Public voidsetData (Object data) { This. data =data; }}
View Code
 Public classConstants {/*** * Delete status*/     Public Static enumdeletestatus{NORMAL ("0", "normal", "OK"), delete ("1", "delete", "delete"); Privatedeletestatus (String value,string name,string desc) { This. value=value;  This. name=name;  This. desc=desc; }        Private FinalString value; Private FinalString name; Private FinalString desc;  PublicString GetValue () {returnvalue; }         PublicString GetName () {returnname; }         PublicString GetDesc () {returndesc; }    }    /*** * Result code*/     Public Static enumResultCode {/**Success*/SUCCESS ("200", "Success"), Null_data ("205", "No Data"),        /**not logged in*/Not_login ("400", "not logged in"),        /**An exception occurred*/EXCEPTION ("401", "Exception occurred"),        /**System Error*/Sys_error ("402", "System error"),        /**parameter Error*/Params_error ("403", "Parameter Error"),        /**not supported or obsolete*/not_supported ("410", "not supported or deprecated"),        /**Authcode Error*/Invalid_authcode ("444", "Invalid Authcode"),        /**calls that are too frequent*/Too_frequent ("445", "too frequent calls"),        /**Unknown error*/Unknown_error ("499", "Unknown Error"),               PrivateResultCode (string value, String msg) { This. val =value;  This. msg =msg; }         PublicString val () {returnVal; }         PublicString msg () {returnmsg; }        PrivateString Val; PrivateString msg; } }
View Code

Java--ping & Telnet Implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.