Search in sources :

Example 6 with NetworkInterface

use of java.net.NetworkInterface in project tomcat by apache.

the class AbstractEndpoint method getUnlockAddress.

private static InetSocketAddress getUnlockAddress(InetSocketAddress localAddress) throws SocketException {
    if (localAddress.getAddress().isAnyLocalAddress()) {
        // Need a local address of the same type (IPv4 or IPV6) as the
        // configured bind address since the connector may be configured
        // to not map between types.
        InetAddress loopbackUnlockAddress = null;
        InetAddress linkLocalUnlockAddress = null;
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (localAddress.getAddress().getClass().isAssignableFrom(inetAddress.getClass())) {
                    if (inetAddress.isLoopbackAddress()) {
                        if (loopbackUnlockAddress == null) {
                            loopbackUnlockAddress = inetAddress;
                        }
                    } else if (inetAddress.isLinkLocalAddress()) {
                        if (linkLocalUnlockAddress == null) {
                            linkLocalUnlockAddress = inetAddress;
                        }
                    } else {
                        // Use a non-link local, non-loop back address by default
                        return new InetSocketAddress(inetAddress, localAddress.getPort());
                    }
                }
            }
        }
        // all local addresses.
        if (loopbackUnlockAddress != null) {
            return new InetSocketAddress(loopbackUnlockAddress, localAddress.getPort());
        }
        if (linkLocalUnlockAddress != null) {
            return new InetSocketAddress(linkLocalUnlockAddress, localAddress.getPort());
        }
        // Fallback
        return new InetSocketAddress("localhost", localAddress.getPort());
    } else {
        return localAddress;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 7 with NetworkInterface

use of java.net.NetworkInterface in project cw-omnibus by commonsguy.

the class WebServerService method raiseReadyEvent.

private void raiseReadyEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 8 with NetworkInterface

use of java.net.NetworkInterface in project cw-omnibus by commonsguy.

the class WebServerService method raiseReadyEvent.

private void raiseReadyEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 9 with NetworkInterface

use of java.net.NetworkInterface in project cw-omnibus by commonsguy.

the class WebServerService method raiseStartedEvent.

private void raiseStartedEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999");
                }
            }
        }
    } catch (SocketException e) {
        Log.e(getClass().getSimpleName(), "Exception in IP addresses", e);
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 10 with NetworkInterface

use of java.net.NetworkInterface in project webmagic by code4craft.

the class ProxyUtils method getNetworkInterface.

private static String getNetworkInterface() {
    String networkInterfaceName = ">>>> modify networkInterface in us.codecraft.webmagic.utils.ProxyUtils";
    Enumeration<NetworkInterface> enumeration = null;
    try {
        enumeration = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
    while (enumeration.hasMoreElements()) {
        NetworkInterface networkInterface = enumeration.nextElement();
        Enumeration<InetAddress> addr = networkInterface.getInetAddresses();
        while (addr.hasMoreElements()) {
            String s = addr.nextElement().getHostAddress();
            Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
            if (s != null && IPV4_PATTERN.matcher(s).matches()) {
                networkInterfaceName += networkInterface.toString() + "IP:" + s + "\n\n";
            }
        }
    }
    return networkInterfaceName;
}
Also used : SocketException(java.net.SocketException) Pattern(java.util.regex.Pattern) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

NetworkInterface (java.net.NetworkInterface)225 InetAddress (java.net.InetAddress)178 SocketException (java.net.SocketException)112 ArrayList (java.util.ArrayList)42 Inet4Address (java.net.Inet4Address)39 UnknownHostException (java.net.UnknownHostException)36 InterfaceAddress (java.net.InterfaceAddress)32 Inet6Address (java.net.Inet6Address)29 IOException (java.io.IOException)25 Enumeration (java.util.Enumeration)9 InetSocketAddress (java.net.InetSocketAddress)7 HashSet (java.util.HashSet)7 LinkedHashSet (java.util.LinkedHashSet)7 Command (com.android.server.NativeDaemonConnector.Command)6 Test (org.junit.Test)6 DatagramSocket (java.net.DatagramSocket)5 File (java.io.File)4 MulticastSocket (java.net.MulticastSocket)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3