private static String generateId() { String host; try { host = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException ex) { host = "unknown"; } return host + '-' + UUID.randomUUID(); }
/** * Guesses the current host name. */ @Nonnull public static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { return "localhost"; } }
protected String localHostnameImpl() throws UnknownHostException { return InetAddress.getLocalHost().getCanonicalHostName(); }
InetAddress IP=InetAddress.getLocalHost(); System.out.println("IP of my system is := "+IP.getHostAddress());
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost(); System.out.println("Hostname of local machine: " + localMachine.getHostName());
InetAddress IP=InetAddress.getLocalHost(); System.out.println(IP.toString());
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; }
static InetAddress getLocalHost() throws UnknownHostException { InetAddress addr; try { addr = InetAddress.getLocalHost(); } catch (ArrayIndexOutOfBoundsException e) { //this is workaround for mac osx bug see AS7-3223 and JGRP-1404 addr = InetAddress.getByName(null); } return addr; }
@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 static String generateLocalName() { String retval=null; try { retval=shortName(InetAddress.getLocalHost().getHostName()); } catch(Throwable ignored) { } if(retval == null) { try { retval=shortName(InetAddress.getByName(null).getHostName()); } catch(Throwable e) { retval="localhost"; } } long counter=Util.random((long)Short.MAX_VALUE * 2); return retval + "-" + counter; }
/** * Get the ip address of local host. */ public static String getHostAddress() throws SocketException, UnknownHostException { DatagramSocket ds = new DatagramSocket(); ds.connect(InetAddress.getByName(DUMMY_OUT_IP), 80); InetAddress localAddress = ds.getLocalAddress(); if (localAddress.getHostAddress().equals("0.0.0.0")) { localAddress = InetAddress.getLocalHost(); } return localAddress.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 localServer() { try { if (StringUtils.isNotBlank(serverAddress)) { return serverAddress + UtilsAndCommons.CLUSTER_CONF_IP_SPLITER + RunningConfig.getServerPort(); } InetAddress inetAddress = InetAddress.getLocalHost(); serverAddress = inetAddress.getHostAddress(); if (PREFER_HOSTNAME_OVER_IP) { if (inetAddress.getHostName().equals(inetAddress.getCanonicalHostName())) { serverAddress = inetAddress.getHostName(); } else { serverAddress = inetAddress.getCanonicalHostName(); } } return serverAddress + UtilsAndCommons.CLUSTER_CONF_IP_SPLITER + RunningConfig.getServerPort(); } catch (UnknownHostException e) { return "resolve_failed"; } }
public HostnameEvaluator(final boolean preferFQDN) throws UnknownHostException { String address; if (preferFQDN) { try { address = InetAddress.getLocalHost().getCanonicalHostName(); } catch (final Exception e) { address = InetAddress.getLocalHost().getHostName(); } } else { address = InetAddress.getLocalHost().getHostName(); } hostname = new StringQueryResult(address); }
// output on my machine: "192.168.1.102" Socket s = new Socket("192.168.1.1", 80); System.out.println(s.getLocalAddress().getHostAddress()); s.close(); // output on my machine: "127.0.1.1" System.out.println(InetAddress.getLocalHost().getHostAddress());
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"); }
@AllowSysOut public static void main(String[] args) throws UnknownHostException { byte[] addressBytes = InetAddress.getLocalHost().getAddress(); System.out.println( "Raw ip address bytes : " + addressBytes.toString() ); int addressInt = BytesHelper.toInt( addressBytes ); System.out.println( "ip address int : " + addressInt ); String formatted = Integer.toHexString( addressInt ); StringBuilder buf = new StringBuilder( "00000000" ); buf.replace( 8 - formatted.length(), 8, formatted ); String addressHex = buf.toString(); System.out.println( "ip address hex : " + addressHex ); } }
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 static String ip() { String hostname = null; try { hostname = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { LOG.error("local_hostname", e); } return hostname; }
public String getLocalHostName() { try { if (null == m_localHost) { m_localHost = InetAddress.getLocalHost(); } return m_localHost.getHostName(); } catch (UnknownHostException e) { return m_local.getHostName(); } }