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;
}
}
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);
}
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);
}
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);
}
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;
}
Aggregations