Java -- ping & amp; telnet implementation, javaping

Source: Internet
Author: User

Java -- ping & telnet implementation, javaping

Ping & telnet implementation class:

Import org. springframework. data. web. jsonPath; import java. io. IOException; import java.net. *; public class PTUtil {/***** ping operation * @ param hostname * @ param timeout in milliseconds * @ return */public static JsonResult pingResult (String hostname, Integer timeout) {JsonResult jsonResult = new JsonResult (); try {InetAddress address = InetAddress. getByName (hostname); boolean flag = address. isReachable (tim Eout); 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 ();} return jsonResult;}/***** telnet operation * @ param hostname * @ param timeout in milliseconds * @ return */public static JsonResult telnetResult (String hostname, integer port, Integer timeout) {JsonResult jsonResult = new JsonResult (); try {Socket server = new Socket (); InetSocketAddress address = new InetSoc KetAddress (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 ();} return jsonResult ;}}
View Code

Related Classes:

Public class JsonResult {private String code; // The result code is private String message; // The result description is private Object data; public JsonResult () {this. setCode (Constants. resultCode. SUCCESS); this. setMessage (Constants. resultCode. SUCCESS. msg ();} public JsonResult (Constants. resultCode code) {this. setCode (code); this. setMessage (code. msg ();} public JsonResult (Constants. resultCode code, String message) {this. setCode (code); this. setMessage (message);} public JsonResult (Constants. resultCode code, String message, Object data) {this. setCode (code); this. setMessage (message); this. setData (data);} public String getCode () {return code;} public void setCode (Constants. resultCode code) {this. code = code. val (); this. message = code. msg ();} public String getMessage () {return message;} public void setMessage (String message) {this. message = message;} public Object getData () {return data;} public void setData (Object data) {this. data = data ;}}
View Code
Public class Constants {/***** DELETE status */public static enum DeleteStatus {NORMAL ("0", "NORMAL", "NORMAL"), DELETE ("1 ", "DELETE", "DELETE"); private DeleteStatus (String value, String name, String desc) {this. value = value; this. name = name; this. desc = desc;} private final String value; private final String name; private final String desc; public String getValue () {return value;} public String getName () {return name ;} public String getDesc () {return desc ;}}/***** Result code */public static enum ResultCode {/** SUCCESS */SUCCESS ("200 ", "successful"), NULL_DATA ("205", "No data"),/** No Logon */NOT_LOGIN ("400", "No Logon "), /** EXCEPTION */EXCEPTION ("401", "EXCEPTION"),/** System Error */SYS_ERROR ("402", "system error "), /** parameter error */PARAMS_ERROR ("403", "parameter error"),/** Unsupported or obsolete */NOT_SUPPORTED ("410 ", "not supported or discarded"),/** AuthCode Error */INVALID_AUTHCODE ("444", "invalid AuthCode "), /** too frequent calls */TOO_FREQUENT ("445", "too frequent calls"),/** unknown error */UNKNOWN_ERROR ("499 ", "Unknown error"), private ResultCode (String value, String msg) {this. val = value; this. msg = msg;} public String val () {return val;} public String msg () {return msg;} private String val; private String msg ;}}
View Code

 

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.