Java——ping & telnet实现

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

 

posted @ 2018-03-23 18:02  Adina~  阅读(3051)  评论(0编辑  收藏  举报