서버 Ip 포트 번호 가져오기

4992 단어
IP 주소 가져오기
public static String getMachineIP() {
    List ipList = new ArrayList();
    try {
        Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress inetAddress;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
                .nextElement();
            Enumeration addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                inetAddress = (InetAddress) addresses.nextElement();
                if (inetAddress != null && inetAddress.isSiteLocalAddress()) {
                    ipList.add(inetAddress.getHostAddress());
                }
            }
        }
    } catch (SocketException ignore) {
    }
    return ipList.size() > 0 ? ipList.get(0) : InetAddress.getLocalHost().getHostAddress();
}

혹은
public static String getLocalIP() {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            // TODO catch 
            e.printStackTrace();
        }
        byte[] ipAddr = addr.getAddress();
        String ipAddrStr = "";
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr += ".";
            }
            ipAddrStr += ipAddr[i] & 0xFF;
        }
        // System.out.println(ipAddrStr);
        return ipAddrStr;
    }

 
포트 번호 가져오기
/**
 *  tomcat 
 *
 * @param isHttps  https 
 */
private static String getServerPort(boolean isHttps) throws AttributeNotFoundException,
InstanceNotFoundException, MBeanException, ReflectionException {
    MBeanServer mBeanServer = null;
    if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
        mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
    }
    if (mBeanServer == null) {
        logger.debug(" findMBeanServer null");
        return "";
    }
    Set names;
    try {
        names = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
    } catch (Exception e) {
        return "";
    }
    Iterator it = names.iterator();
    ObjectName OName;
    while (it.hasNext()) {
        OName = it.next();
        String protocol = (String) mBeanServer.getAttribute(OName, "protocol");
        String scheme = (String) mBeanServer.getAttribute(OName, "scheme");
        Boolean secureValue = (Boolean) mBeanServer.getAttribute(OName, "secure");
        Boolean SSLEnabled = (Boolean) mBeanServer.getAttribute(OName, "SSLEnabled");
        if (SSLEnabled != null && SSLEnabled) {// tomcat6 SSLEnabled
            secureValue = true;// SSLEnabled=true secure 
            scheme = "https";
        }
        if (protocol != null && ("HTTP/1.1".equals(protocol) || protocol.contains("http"))) {
            if (isHttps && "https".equals(scheme) && secureValue) {
                return (mBeanServer.getAttribute(OName, "port")).toString();
            } else if (!isHttps && !"https".equals(scheme) && !secureValue) {
                return (mBeanServer.getAttribute(OName, "port")).toString();
            }
        }
    }
    return "";
}

혹은
public static final int getTomcatPort(){
    try{
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        ObjectName name = new ObjectName("Catalina", "type", "Server");
        Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
        Service[] services = server.findServices();
        for (Service service : services) {
            Connector connector = Iterables.find(Arrays.asList(service.findConnectors()), new Predicate() {
                @Override
                public boolean apply(Connector input) {
                    ProtocolHandler protocolHandler = input.getProtocolHandler();
                    return protocolHandler instanceof Http11AprProtocol
                            || protocolHandler instanceof Http11NioProtocol;
                }
            });
            return connector.getPort();
        }
    }catch (Exception e){
        LOGGER.error(" tomcat ",e);
    }
    return 8080;
}  

또는
Set objectNames = beanServer.queryNames(
         new ObjectName("*:type=Connector,*"),
         Query.match(Query.attr("protocol"),Query.value("HTTP/1.1")));
String port = objectNames.iterator().next().getKeyProperty("port");

원문 작성자 링크https://www.jianshu.com/p/71352e15966f

좋은 웹페이지 즐겨찾기