1. 程式人生 > >Java——ping & telnet實現

Java——ping & telnet實現

unknown not out ddr gpo second 技術分享 net new

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

Java——ping & telnet實現