List of usage examples for java.net InetAddress getHostName
public String getHostName()
From source file:net.grinder.util.NetworkUtil.java
/** * Get local host name by connecting to a server. * //w ww . ja va2s.c o m * @param byConnecting * the server address to connect. * @param port * the port to connect * @return localhost name. if fails, return "localhost" */ public static String getLocalHostName(String byConnecting, int port) { InetAddress addr = getLocalInetAddress(byConnecting, port); if (addr != null) { return addr.getHostName(); } else { return "localhost"; } }
From source file:org.ntpsync.util.NtpSyncUtils.java
/** * Queries NTP server using UDP to get offset * //from w ww .j a v a2s . c o m * @param ntpServerHostname * @return offset * @throws IOException * , SocketException */ public static long query(String ntpServerHostname) throws IOException, SocketException { NTPUDPClient client = new NTPUDPClient(); // We want to timeout if a response takes longer than 10 seconds client.setDefaultTimeout(10000); TimeInfo info = null; try { client.open(); InetAddress hostAddr = InetAddress.getByName(ntpServerHostname); Log.d(Constants.TAG, "Trying to get time from " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress()); info = client.getTime(hostAddr); } finally { client.close(); } // compute offset/delay if not already done info.computeDetails(); return info.getOffset(); }
From source file:com.collabnet.tracker.common.httpClient.TrackerHttpSender.java
private static Credentials getCredentials(final String username, final String password, final InetAddress address) { int i = username.indexOf("\\"); if (i > 0 && i < username.length() - 1 && address != null) { return new NTCredentials(username.substring(i + 1), password, address.getHostName(), username.substring(0, i)); } else {/*from www . j a v a 2s. c o m*/ return new UsernamePasswordCredentials(username, password); } }
From source file:com.alibaba.jstorm.utils.NetWorkUtils.java
public static String ip2Host(String ip) { InetAddress address = null; try {/*from w w w . j ava 2s .com*/ address = InetAddress.getByName(ip); } catch (UnknownHostException e) { LOG.warn("NetWorkUtil can't transfer ip(" + ip + ") to hostname, return ip", e); return ip; } return address.getHostName(); }
From source file:org.ntpsync.util.NtpSyncUtils.java
/** * Process <code>TimeInfo</code> object and print its details. * /* ww w . j a va 2 s . c o m*/ * @param info * <code>TimeInfo</code> object. */ public static String processResponse(TimeInfo info, Context context) { String output = ""; NtpV3Packet message = info.getMessage(); int stratum = message.getStratum(); String refType; if (stratum <= 0) { refType = context.getString(R.string.detailed_query_unspecified); } else if (stratum == 1) { refType = context.getString(R.string.detailed_query_primary_reference); // GPS, radio // clock, etc. } else { refType = context.getString(R.string.detailed_query_secondary_reference); } // stratum should be 0..15... output += "<p><b>" + context.getString(R.string.detailed_query_server) + "</b><br/>" + context.getString(R.string.detailed_query_stratum) + " " + stratum + " " + refType; int version = message.getVersion(); int li = message.getLeapIndicator(); output += "<br/>" + context.getString(R.string.detailed_query_leap) + " " + li + "<br/>" + context.getString(R.string.detailed_query_version) + " " + version + "<br/>" + context.getString(R.string.detailed_query_precision) + " " + message.getPrecision(); output += "<br/>" + context.getString(R.string.detailed_query_mode) + " " + message.getModeName() + " (" + message.getMode() + ")"; int poll = message.getPoll(); // poll value typically btwn MINPOLL (4) and MAXPOLL (14) output += "<br/>" + context.getString(R.string.detailed_query_poll) + " " + (poll <= 0 ? 1 : (int) Math.pow(2, poll)) + " " + context.getString(R.string.detailed_query_seconds) + " (2 ** " + poll + ")"; double disp = message.getRootDispersionInMillisDouble(); output += "<br/>" + context.getString(R.string.detailed_query_rootdelay) + " " + numberFormat.format(message.getRootDelayInMillisDouble()) + "<br/>" + context.getString(R.string.detailed_query_rootdispersion) + " " + numberFormat.format(disp); int refId = message.getReferenceId(); String refAddr = NtpUtils.getHostAddress(refId); String refName = null; if (refId != 0) { if (refAddr.equals("127.127.1.0")) { refName = "LOCAL"; // This is the ref address for the Local Clock } else if (stratum >= 2) { // If reference id has 127.127 prefix then it uses its own reference clock // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5 // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution. if (!refAddr.startsWith("127.127")) { try { InetAddress addr = InetAddress.getByName(refAddr); String name = addr.getHostName(); if (name != null && !name.equals(refAddr)) { refName = name; } } catch (UnknownHostException e) { // some stratum-2 servers sync to ref clock device but fudge stratum level // higher... (e.g. 2) // ref not valid host maybe it's a reference clock name? // otherwise just show the ref IP address. refName = NtpUtils.getReferenceClock(message); } } } else if (version >= 3 && (stratum == 0 || stratum == 1)) { refName = NtpUtils.getReferenceClock(message); // refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.) } // otherwise give up on naming the beast... } if (refName != null && refName.length() > 1) { refAddr += " (" + refName + ")"; } output += "<p><b>" + context.getString(R.string.detailed_query_reference_identifier) + "</b><br/>" + refAddr + "</p>"; TimeStamp refNtpTime = message.getReferenceTimeStamp(); output += "<p><b>" + context.getString(R.string.detailed_query_reference_timestamp) + "</b><br/>" + refNtpTime.toDateString() + "</p>"; // Originate Time is time request sent by client (t1) TimeStamp origNtpTime = message.getOriginateTimeStamp(); output += "<p><b>" + context.getString(R.string.detailed_query_originate_timestamp) + "</b><br/>" + origNtpTime.toDateString() + "</p>"; long destTime = info.getReturnTime(); // Receive Time is time request received by server (t2) TimeStamp rcvNtpTime = message.getReceiveTimeStamp(); output += "<p><b>" + context.getString(R.string.detailed_query_receive_timestamp) + "</b><br/>" + rcvNtpTime.toDateString() + "</p>"; // Transmit time is time reply sent by server (t3) TimeStamp xmitNtpTime = message.getTransmitTimeStamp(); output += "<p><b>" + context.getString(R.string.detailed_query_transmit_timestamp) + "</b><br/>" + xmitNtpTime.toDateString() + "</p>"; // Destination time is time reply received by client (t4) TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime); output += "<p><b>" + context.getString(R.string.detailed_query_destination_timestamp) + "</b><br/>" + destNtpTime.toDateString() + "</p>"; info.computeDetails(); // compute offset/delay if not already done Long offsetValue = info.getOffset(); Long delayValue = info.getDelay(); String delay = (delayValue == null) ? "N/A" : delayValue.toString(); String offset = (offsetValue == null) ? "N/A" : offsetValue.toString(); // offset in ms output += "<p><b>" + context.getString(R.string.detailed_query_computed_offset) + "</b><br/>" + context.getString(R.string.detailed_query_roundtrip_delay) + " " + delay + "<br/>" + context.getString(R.string.detailed_query_clock_offset) + " " + offset + "</p>"; return output; }
From source file:com.eviware.soapui.impl.wsdl.support.http.ProxyUtils.java
private static String nslookup(String s, boolean ip) { InetAddress host; String address;/*from w w w. j ava 2 s . c om*/ // get the bytes of the IP address try { host = InetAddress.getByName(s); if (ip) address = host.getHostAddress(); else address = host.getHostName(); } catch (UnknownHostException ue) { return s; // no host } return address; }
From source file:SocketFetcher.java
/** * Start TLS on an existing socket. Supports the "STARTTLS" command in many * protocols.//from w w w . j ava2 s . c o m */ public static Socket startTLS(Socket socket, Properties props, String prefix) throws IOException { InetAddress a = socket.getInetAddress(); String host = a.getHostName(); int port = socket.getPort(); // System.out.println("SocketFetcher: startTLS host " + host + ", port " + // port); try { SSLSocketFactory ssf; String sfClass = props.getProperty(prefix + ".socketFactory.class", null); SocketFactory sf = getSocketFactory(sfClass); if (sf != null && sf instanceof SSLSocketFactory) ssf = (SSLSocketFactory) sf; else ssf = (SSLSocketFactory) SSLSocketFactory.getDefault(); socket = ssf.createSocket(socket, host, port, true); configureSSLSocket(socket, props, prefix); } catch (Exception ex) { if (ex instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException) ex).getTargetException(); if (t instanceof Exception) ex = (Exception) t; } if (ex instanceof IOException) throw (IOException) ex; // wrap anything else before sending it on IOException ioex = new IOException( "Exception in startTLS: host " + host + ", port " + port + "; Exception: " + ex); throw ioex; } return socket; }
From source file:org.apache.hadoop.hbase.zookeeper.HQuorumPeer.java
static void writeMyID(Properties properties) throws IOException { long myId = -1; Configuration conf = HBaseConfiguration.create(); String myAddress = DNS.getDefaultHost(conf.get("hbase.zookeeper.dns.interface", "default"), conf.get("hbase.zookeeper.dns.nameserver", "default")); List<String> ips = new ArrayList<String>(); // Add what could be the best (configured) match ips.add(myAddress.contains(".") ? myAddress : StringUtils.simpleHostname(myAddress)); // For all nics get all hostnames and IPs Enumeration<?> nics = NetworkInterface.getNetworkInterfaces(); while (nics.hasMoreElements()) { Enumeration<?> rawAdrs = ((NetworkInterface) nics.nextElement()).getInetAddresses(); while (rawAdrs.hasMoreElements()) { InetAddress inet = (InetAddress) rawAdrs.nextElement(); ips.add(StringUtils.simpleHostname(inet.getHostName())); ips.add(inet.getHostAddress()); }/*from w w w . j a v a 2 s . c o m*/ } for (Entry<Object, Object> entry : properties.entrySet()) { String key = entry.getKey().toString().trim(); String value = entry.getValue().toString().trim(); if (key.startsWith("server.")) { int dot = key.indexOf('.'); long id = Long.parseLong(key.substring(dot + 1)); String[] parts = value.split(":"); String address = parts[0]; if (addressIsLocalHost(address) || ips.contains(address)) { myId = id; break; } } } if (myId == -1) { throw new IOException( "Could not find my address: " + myAddress + " in list of ZooKeeper quorum servers"); } String dataDirStr = properties.get("dataDir").toString().trim(); File dataDir = new File(dataDirStr); if (!dataDir.isDirectory()) { if (!dataDir.mkdirs()) { throw new IOException("Unable to create data dir " + dataDir); } } File myIdFile = new File(dataDir, "myid"); PrintWriter w = new PrintWriter(myIdFile); w.println(myId); w.close(); }
From source file:SocketFetcher.java
/** * Start TLS on an existing socket./*from w ww .jav a 2 s .com*/ * Supports the "STARTTLS" command in many protocols. */ public static Socket startTLS(Socket socket, Properties props, String prefix) throws IOException { InetAddress a = socket.getInetAddress(); String host = a.getHostName(); int port = socket.getPort(); //System.out.println("SocketFetcher: startTLS host " + host + ", port " + port); try { SSLSocketFactory ssf; String sfClass = props.getProperty(prefix + ".socketFactory.class", null); SocketFactory sf = getSocketFactory(sfClass); if (sf != null && sf instanceof SSLSocketFactory) ssf = (SSLSocketFactory) sf; else ssf = (SSLSocketFactory) SSLSocketFactory.getDefault(); socket = ssf.createSocket(socket, host, port, true); configureSSLSocket(socket, props, prefix); } catch (Exception ex) { if (ex instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException) ex).getTargetException(); if (t instanceof Exception) ex = (Exception) t; } if (ex instanceof IOException) throw (IOException) ex; // wrap anything else before sending it on IOException ioex = new IOException( "Exception in startTLS: host " + host + ", port " + port + "; Exception: " + ex); ioex.initCause(ex); throw ioex; } return socket; }
From source file:net.sf.jasperreports.phantomjs.InetUtil.java
public static Inet4Address getIPv4Loopback() { InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); //the phantomjs web server module only works with IPv4 if (loopbackAddress instanceof Inet4Address) { return (Inet4Address) loopbackAddress; }//from w ww . ja v a2 s .c om try { InetAddress[] addresses = InetAddress.getAllByName(loopbackAddress.getHostName()); for (InetAddress inetAddress : addresses) { if (inetAddress instanceof Inet4Address) { return (Inet4Address) inetAddress; } } } catch (UnknownHostException e) { log.warn("Error while determining loopback addresses for " + loopbackAddress.getHostName(), e); } try { //keep looking for a IPv4 loopback address for (Enumeration<NetworkInterface> itfs = NetworkInterface.getNetworkInterfaces(); itfs .hasMoreElements();) { NetworkInterface itf = itfs.nextElement(); if (itf.isLoopback()) { for (Enumeration<InetAddress> addresses = itf.getInetAddresses(); addresses .hasMoreElements();) { InetAddress address = addresses.nextElement(); if (address instanceof Inet4Address) { return (Inet4Address) address; } } } } } catch (SocketException e) { log.warn("Error while listing network interfaces", e); } return null; }