Java——ping & telnet實現,javaping

來源:互聯網
上載者:User

Java——ping & telnet實現,javaping

ping & telnet 實作類別:

import org.springframework.data.web.JsonPath;import java.io.IOException;import java.net.*;public class PTUtil {    /***     *  ping操作     * @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(timeout);            if(flag){                jsonResult.setMessage("ping結果:the address is reachable.");            }else{                jsonResult.setCode(Constants.ResultCode.EXCEPTION);                jsonResult.setMessage("ping結果:the address is unreachable.");            }        } catch (UnknownHostException e) {            jsonResult.setCode(Constants.ResultCode.EXCEPTION);            jsonResult.setMessage("ping結果:UnknownHostException:"+e.getMessage());        } catch (IOException e) {            jsonResult.setCode(Constants.ResultCode.EXCEPTION);            jsonResult.setMessage("ping結果:IOException:"+e.getMessage());        }        return jsonResult;    }    /***     *  telnet 操作     * @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 InetSocketAddress(hostname,port);            server.connect(address, timeout);            server.close();            jsonResult.setMessage("telnet結果:success!");        }catch (UnknownHostException e) {            jsonResult.setCode(Constants.ResultCode.EXCEPTION);            jsonResult.setMessage("telnet結果:UnknownHostException:"+e.getMessage());        } catch (IOException e) {            jsonResult.setCode(Constants.ResultCode.EXCEPTION);            jsonResult.setMessage("telnet結果:IOException:"+e.getMessage());        }        return jsonResult;    } }
View Code

相關類:

public class JsonResult {        private String code;//結果編碼    private String message;//結果描述    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 {    /***     *刪除狀態     */    public static enum DeleteStatus{        NORMAL("0","NORMAL","正常"),DELETE("1","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("200", "成功"),        NULL_DATA("205","無資料"),        /** 沒有登入 */        NOT_LOGIN("400", "沒有登入"),        /** 發生異常 */        EXCEPTION("401", "發生異常"),        /** 系統錯誤 */        SYS_ERROR("402", "系統錯誤"),        /** 參數錯誤 */        PARAMS_ERROR("403", "參數錯誤 "),        /** 不支援或已經廢棄 */        NOT_SUPPORTED("410", "不支援或已經廢棄"),        /** AuthCode錯誤 */        INVALID_AUTHCODE("444", "無效的AuthCode"),        /** 太頻繁的調用 */        TOO_FREQUENT("445", "太頻繁的調用"),        /** 未知的錯誤 */        UNKNOWN_ERROR("499", "未知錯誤"),               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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.