zoukankan      html  css  js  c++  java
  • java判断ip和端口是否通信正常

    package com.client;
    
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class TestIpPort {
    
        public static void main(String[] args) {
            System.out.println(isHostConnectable("10.1.11.10", 16181));
        }
        
        public static boolean isHostConnectable(String host, int port) {
            Socket socket = new Socket();
            try {
                socket.connect(new InetSocketAddress(host, port));
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
        
        public static boolean isHostReachable(String host, Integer timeOut) {
            try {
                return InetAddress.getByName(host).isReachable(timeOut);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    }
  • 相关阅读:
    maven
    ELK
    gitlab 升级
    平安工作流程
    平安云应用场景
    nginx基于uwsgi部署Django (单机搭建)
    ansible
    nginx理论
    GIT
    docker(三)
  • 原文地址:https://www.cnblogs.com/liangblog/p/14377653.html
Copyright © 2011-2022 走看看