/** * @throws IgniteSpiException If any error occurs. * @return {@code true} if IP finder contains local address. */ boolean ipFinderHasLocalAddress() throws IgniteSpiException { for (InetSocketAddress locAddr : locNodeAddrs) { for (InetSocketAddress addr : registeredAddresses()) try { int port = addr.getPort(); InetSocketAddress resolved = addr.isUnresolved() ? new InetSocketAddress(InetAddress.getByName(addr.getHostName()), port) : new InetSocketAddress(addr.getAddress(), port); if (resolved.equals(locAddr)) return true; } catch (UnknownHostException e) { getExceptionRegistry().onException(e.getMessage(), e); } } return false; }
InetAddress candidateAddress = null; for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) { NetworkInterface ni = (NetworkInterface) interfaces.nextElement(); for (Enumeration inetAddrs = ni.getInetAddresses(); inetAddrs.hasMoreElements(); ) { InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); if (!inetAddr.isLoopbackAddress()) { if (inetAddr.isSiteLocalAddress()) { return inetAddr; } else if (candidateAddress == null) { InetAddress jdkSuppliedAddress = InetAddress.getLocalHost(); if (jdkSuppliedAddress == null) { throw new UnknownHostException( "The JDK InetAddress.getLocalHost() method unexpectedly returned null."); } catch (Exception e) { UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address"); unknownHostException.initCause(e); throw unknownHostException;
/** * 建立远程连接并发送命令 * * @param command * @param server * @param port */ private static void doSend(String command, String server, int port) { Socket socket = null; try { socket = new Socket(server, port); OutputStream os = socket.getOutputStream(); BufferedOutputStream out = new BufferedOutputStream(os); out.write(command.getBytes()); out.write('\r'); out.flush(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null && !socket.isClosed()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public UnknownHostException rethrowAsUnknownHostException(String detailMessage) throws UnknownHostException { UnknownHostException newException = new UnknownHostException(detailMessage); newException.initCause(this); throw newException; }
@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException { if (hostname == null) throw new UnknownHostException("hostname == null"); InetAddress inetAddress = hosts.get(hostname); if (inetAddress != null) { return Collections.singletonList(inetAddress); } if (Settings.getBuiltInHosts()) { inetAddress = builtInHosts.get(hostname); if (inetAddress != null) { return Collections.singletonList(inetAddress); } } try { return Arrays.asList(InetAddress.getAllByName(hostname)); } catch (NullPointerException e) { UnknownHostException unknownHostException = new UnknownHostException("Broken system behaviour for dns lookup of " + hostname); unknownHostException.initCause(e); throw unknownHostException; } } }
private static String getLocalAddressAsString() throws UnknownHostException, SocketException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces != null && interfaces.hasMoreElements()) { Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses(); while (addresses != null && addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (acceptableAddress(address)) { return address.getHostAddress(); } } } throw new UnknownHostException(); }
NetworkInterface bind_intf=null; if(bind_addr != null && bind_addr.isLoopbackAddress()) return bind_addr; throw new UnknownHostException("network interface " + bind_interface_str + " not found"); Enumeration addresses=bind_intf.getInetAddresses(); while(addresses != null && addresses.hasMoreElements()) { InetAddress address=(InetAddress)addresses.nextElement(); if(bind_addr.equals(address)) { hasAddress=true; break; String bind_addr_str=bind_addr.getHostAddress(); throw new IllegalArgumentException("network interface " + bind_interface_str + " does not contain address " + bind_addr_str); if(bind_addr != null && NetworkInterface.getByInetAddress(bind_addr) == null) { throw new UnknownHostException("Invalid bind address " + bind_addr);
/** * A function to check if an interface supports an IP version (i.e has addresses * defined for that IP version). * @param intf * @return */ public static boolean interfaceHasIPAddresses(NetworkInterface intf,StackType ip_version) throws UnknownHostException { boolean supportsVersion=false; if(intf != null) { // get all the InetAddresses defined on the interface Enumeration addresses=intf.getInetAddresses(); while(addresses != null && addresses.hasMoreElements()) { // get the next InetAddress for the current interface InetAddress address=(InetAddress)addresses.nextElement(); // check if we find an address of correct version if((address instanceof Inet4Address && (ip_version == StackType.IPv4)) || (address instanceof Inet6Address && (ip_version == StackType.IPv6))) { supportsVersion=true; break; } } } else { throw new UnknownHostException("network interface " + intf + " not found"); } return supportsVersion; }
Enumeration<InetAddress> addressList = nif.getInetAddresses(); while (addressList.hasMoreElements()) { InetAddress ia = addressList.nextElement(); if (ia.getAddress().length == 16) { Inet6Address v6ia = (Inet6Address) ia; boolean isSameType = v6ia.compareLocalType(address); address.scope_id = v6ia.scope_id; address.scope_ifname_set = true; address.ifname = nif.getName(); break; throw new UnknownHostException("Scope id not found for address: " + Arrays.toString(addr));
public static void checkConnectivity(String host) throws IOException { InetAddress headUnitAddr = InetAddress.getByName(host); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); NetworkInterface iface; while (interfaces.hasMoreElements()) { iface = interfaces.nextElement(); if (headUnitAddr.isReachable(iface, 0, 5000)) { return; } } throw new UnknownHostException("Connectivity check to the host \"" + host + "\" has failed"); }
anyhost = true; try { hostToBind = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { logger.warn(e.getMessage(), e); try (Socket socket = new Socket()) { SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort()); socket.connect(addr, 1000); hostToBind = socket.getLocalAddress().getHostAddress(); break; } catch (Exception e) {
if (_proxy != null) try { _proxy.close(); } catch (IOException ioe) {} String hostlc = host.toLowerCase(Locale.US); if (hostlc.endsWith(".i2p")) throw new UnknownHostException("I2P addresses must be proxied"); if (hostlc.endsWith(".onion")) throw new UnknownHostException("Tor addresses must be proxied"); int port = url.getPort(); if (port == -1) port = 80; if (_fetchHeaderTimeout > 0) { _proxy = new Socket(); _proxy.setSoTimeout(_fetchHeaderTimeout); _proxy.connect(new InetSocketAddress(host, port), _fetchHeaderTimeout); } else { _proxy = new Socket(host, port);
public void connect() throws IllegalStateException, IOException { if (isConnected()) { throw new IllegalStateException("Already connected"); } InetSocketAddress address = this.address; if (address == null) { address = new InetSocketAddress(hostname, port); } if (address.getAddress() == null) { throw new UnknownHostException(address.getHostName()); } this.socket = socketFactory.createSocket(address.getAddress(), address.getPort()); this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), charset)); }
public static String getLocalhostNameOrRandomNameIfNotFound() { if (hostName != null) { return hostName; } try { hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { hostName = "unknown-host-" + Math.abs(new Random(System.currentTimeMillis()).nextInt()) % 1000; LOG.warn("Unable to resolve hostname: {}. Using: {}", e.getMessage(), hostName); } return hostName; }
/** * Get IP of the System. * * @return IP of the System */ public static String getIP() { InetAddress ip = null; try { ip = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return ip.getHostName() + "@" + ip.getHostAddress(); }
public static String get() { try { final InetAddress addr = InetAddress.getLocalHost(); return addr.getHostName(); } catch (UnknownHostException e) { e.printStackTrace(); return "hostname-unknown"; } } }
private void init(){ try { socket = new Socket(host, port); output = new ObjectOutputStream(socket.getOutputStream()); input = new ObjectInputStream(socket.getInputStream()); localAddress = socket.getLocalAddress().getHostAddress(); } catch (UnknownHostException e) { exception = e; e.printStackTrace(); } catch (IOException e) { exception = e; e.printStackTrace(); } }
/** * Construct the service key for a token * @param addr InetSocketAddress of remote connection with a token * @return "ip:port" or "host:port" depending on the value of * hadoop.security.token.service.use_ip */ public static Text buildTokenService(InetSocketAddress addr) { String host = null; if (useIpForTokenService) { if (addr.isUnresolved()) { // host has no ip address throw new IllegalArgumentException( new UnknownHostException(addr.getHostName()) ); } host = addr.getAddress().getHostAddress(); } else { host = StringUtils.toLowerCase(addr.getHostName()); } return new Text(host + ":" + addr.getPort()); }
private String getHost() { try { return InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); return "127.0.0.1"; } } }
socketPort = proxySocketAddress.getPort(); inetSocketAddresses.add(InetSocketAddress.createUnresolved(socketHost, socketPort)); } else { eventListener.dnsStart(call, socketHost); throw new UnknownHostException(address.dns() + " returned no addresses for " + socketHost); inetSocketAddresses.add(new InetSocketAddress(inetAddress, socketPort));