public String getLocalHostName() { try { if (null == m_localHost) { m_localHost = InetAddress.getLocalHost(); } return m_localHost.getHostName(); } catch (UnknownHostException e) { return m_local.getHostName(); } }
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost(); System.out.println("Hostname of local machine: " + localMachine.getHostName());
/** * Get the host name of a local address, if available. * * @param localAddress the local address * @return the host name, or another text if not available */ public static String getHostName(String localAddress) { try { InetAddress addr = InetAddress.getByName(localAddress); return addr.getHostName(); } catch (Exception e) { return "unknown"; } }
InetAddress addr = InetAddress.getByName("192.168.190.62"); String host = addr.getHostName(); System.out.println(host);
public void go() throws IOException { final ServerSocket s = new ServerSocket(listenPort); final ExecutorService exe = Executors.newCachedThreadPool(); while (true) { final Socket incoming = s.accept(); ip = incoming.getLocalAddress().getHostAddress(); System.out.println("New Client Connected from " + incoming.getInetAddress().getHostName() + "... "); exe.submit(new FtpLoop(incoming, this)); } }
public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()) { NetworkInterface i=en.nextElement(); System.out.println(i.getName() + ':'); System.out.println(" \t" + i.getDisplayName()); for(Enumeration<InetAddress> en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=en2.nextElement(); System.out.println(" \t" + addr + " (" + addr.getHostName() + ')'); } System.out.println("---------------------"); } }
public HostInfo() { String hostName; String hostAddress; try { InetAddress localhost = InetAddress.getLocalHost(); hostName = localhost.getHostName(); hostAddress = localhost.getHostAddress(); } catch (UnknownHostException e) { hostName = "localhost"; hostAddress = "127.0.0.1"; } HOST_NAME = hostName; HOST_ADDRESS = hostAddress; }
private boolean isAllowedToImpersonateFromHost(Set<String> authorizedHosts, InetAddress remoteAddress) { return authorizedHosts.contains(WILD_CARD) || authorizedHosts.contains(remoteAddress.getCanonicalHostName()) || authorizedHosts.contains(remoteAddress.getHostName()) || authorizedHosts.contains(remoteAddress.getHostAddress()); }
private static String canonicalizeServiceHostName(String hostName) { try { InetAddress address = InetAddress.getByName(hostName); String fullHostName; if ("localhost".equalsIgnoreCase(address.getHostName())) { fullHostName = InetAddress.getLocalHost().getCanonicalHostName(); } else { fullHostName = address.getCanonicalHostName(); } if (fullHostName.equalsIgnoreCase("localhost")) { throw new ClientException("Fully qualified name of localhost should not resolve to 'localhost'. System configuration error?"); } return fullHostName; } catch (UnknownHostException e) { throw new ClientException("Failed to resolve host: " + hostName, e); } }
public static String resolveIP( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { String sRC = ""; if ( ArgList.length == 2 ) { try { InetAddress addr = InetAddress.getByName( (String) ArgList[0] ); if ( ( (String) ArgList[1] ).equals( "IP" ) ) { sRC = addr.getHostName(); } else { sRC = addr.getHostAddress(); } if ( sRC.equals( ArgList[0] ) ) { sRC = "-"; } } catch ( Exception e ) { sRC = "-"; } } else { throw new RuntimeException( "The function call resolveIP requires 2 arguments." ); } return sRC; }
@Test public void shouldDetermineIfAddressIsLocal() throws UnknownHostException { InetAddress local; try { local = InetAddress.getLocalHost(); } catch (UnknownHostException e) { local = InetAddress.getByName("localhost"); } assertThat("Localhost (" + local.getHostName() + ") should be a local address.", SystemUtil.isLocalhost(local.getHostAddress()), is(true)); }
public void run() { logger.info("Starting server."); try { server = new ServerSocket(MaryProperties.needInteger("socket.port")); while (true) { logger.info("Waiting for client to connect on port " + server.getLocalPort()); Socket client = server.accept(); logger.info("Connection from " + client.getInetAddress().getHostName() + " (" + client.getInetAddress().getHostAddress() + ")."); clients.execute(new ClientHandler(client)); } } catch (Exception e) { throw new RuntimeException(e); } }
@Override public String getHostName() throws UnknownHostException { return socket.getInetAddress().getHostName(); }
import java.net.InetAddress; import java.net.UnknownHostException; String hostname = "Unknown"; try { InetAddress addr; addr = InetAddress.getLocalHost(); hostname = addr.getHostName(); } catch (UnknownHostException ex) { System.out.println("Hostname can not be resolved"); }
public HostInfo() { String hostName; String hostAddress; try { InetAddress localhost = InetAddress.getLocalHost(); hostName = localhost.getHostName(); hostAddress = localhost.getHostAddress(); } catch (UnknownHostException e) { hostName = "localhost"; hostAddress = "127.0.0.1"; } HOST_NAME = hostName; HOST_ADDRESS = hostAddress; }
public HTTPSession(NanoHTTPD httpd, ITempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { this.httpd = httpd; this.tempFileManager = tempFileManager; this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE); this.outputStream = outputStream; this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString(); this.remoteHostname = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "localhost" : inetAddress.getHostName().toString(); this.headers = new HashMap<String, String>(); }
/** * Guesses the current host name. */ @Nonnull public static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { return "localhost"; } }
private static String canonicalizeServiceHostName(String hostName) { try { InetAddress address = InetAddress.getByName(hostName); String fullHostName; if ("localhost".equalsIgnoreCase(address.getHostName())) { fullHostName = InetAddress.getLocalHost().getCanonicalHostName(); } else { fullHostName = address.getCanonicalHostName(); } if (fullHostName.equalsIgnoreCase("localhost")) { throw new ClientException("Fully qualified name of localhost should not resolve to 'localhost'. System configuration error?"); } return fullHostName; } catch (UnknownHostException e) { throw new ClientException("Failed to resolve host: " + hostName, e); } }
String reverseDNS(InetAddress ipAddress) throws UnknownHostException { String hostName = this.reverseDNSCacheMap.get(ipAddress); if (hostName == null) { String ipAddressString = null; try { ipAddressString = DNS.reverseDns(ipAddress, null); } catch (Exception e) { // We can use InetAddress in case the jndi failed to pull up the reverse DNS entry from the // name service. Also, in case of ipv6, we need to use the InetAddress since resolving // reverse DNS using jndi doesn't work well with ipv6 addresses. ipAddressString = InetAddress.getByName(ipAddress.getHostAddress()).getHostName(); } if (ipAddressString == null) throw new UnknownHostException("No host found for " + ipAddress); hostName = Strings.domainNamePointerToHostName(ipAddressString); this.reverseDNSCacheMap.put(ipAddress, hostName); } return hostName; }
String hostname1 = InetAddress.getLocalHost().getHostName(); String host1 = InetAddress.getLocalHost().getHostAddress(); String hostname2 = "neo4j.org"; assertDoesNotThrow( () -> InetAddress.getByName( hostname2 ) ); assumeFalse( hostname1.equals( hostname2 ) );