java-检查IP和端口在超时时间内是否可以连接

检查IP和端口在超时时间内是否可以连接

/**
     * 检查IP和端口是否可以连接
     * @param ip
     * @param port
     * @return
     */
    private static boolean testConnect(String ip,int port) {
        Boolean isConnect = false;
        Socket connect = new Socket();
        try {
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
            connect.connect(inetSocketAddress, 1500);
            isConnect = connect.isConnected();
        } catch (IOException e) {
            
        }finally {
            if (connect != null) {
                try {
                    connect.close();
                } catch (IOException e) {
                }
            }
        }
        return isConnect;
    }

 

posted on 2019-05-09 19:12  LJD泊水  阅读(595)  评论(0编辑  收藏  举报