當前位置: 首頁>>代碼示例>>Java>>正文


Java InetAddress.getCanonicalHostName方法代碼示例

本文整理匯總了Java中java.net.InetAddress.getCanonicalHostName方法的典型用法代碼示例。如果您正苦於以下問題:Java InetAddress.getCanonicalHostName方法的具體用法?Java InetAddress.getCanonicalHostName怎麽用?Java InetAddress.getCanonicalHostName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.InetAddress的用法示例。


在下文中一共展示了InetAddress.getCanonicalHostName方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: replaceStaticString

import java.net.InetAddress; //導入方法依賴的package包/類
/**
 * Not intended as a public API
 */
@VisibleForTesting
protected static String replaceStaticString(String key) {
  String replacementString = "";
  try {
    InetAddress addr = InetAddress.getLocalHost();
    switch (key.toLowerCase()) {
      case "localhost":
        replacementString = addr.getHostName();
        break;
      case "ip":
        replacementString = addr.getHostAddress();
        break;
      case "fqdn":
        replacementString = addr.getCanonicalHostName();
        break;
      default:
        throw new RuntimeException("The static escape string '" + key + "'"
                + " was provided but does not match any of (localhost,IP,FQDN)");
    }
  } catch (UnknownHostException e) {
    throw new RuntimeException("Flume wasn't able to parse the static escape "
            + " sequence '" + key + "' due to UnkownHostException.", e);
  }
  return replacementString;
}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:29,代碼來源:BucketPath.java

示例2: HostInterceptor

import java.net.InetAddress; //導入方法依賴的package包/類
/**
 * Only {@link HostInterceptor.Builder} can build me
 */
private HostInterceptor(boolean preserveExisting,
    boolean useIP, String header) {
  this.preserveExisting = preserveExisting;
  this.header = header;
  InetAddress addr;
  try {
    addr = InetAddress.getLocalHost();
    if (useIP) {
      host = addr.getHostAddress();
    } else {
      host = addr.getCanonicalHostName();
    }
  } catch (UnknownHostException e) {
    logger.warn("Could not get local host address. Exception follows.", e);
  }

}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:21,代碼來源:HostInterceptor.java

示例3: getServerBindAddressAsString

import java.net.InetAddress; //導入方法依賴的package包/類
/**
 * Gets the host, as either hostname or IP address, on which the Server was bound and running. An
 * attempt is made to get the canonical hostname for IP address to which the Server was bound for
 * accepting client requests. If the server bind address is null or localhost is unknown, then a
 * default String value of "localhost/127.0.0.1" is returned.
 * 
 * Note, this information is purely information and should not be used to re-construct state or
 * for other purposes.
 * 
 * @return the hostname or IP address of the host running the Server, based on the bind-address,
 *         or 'localhost/127.0.0.1' if the bind address is null and localhost is unknown.
 * @see java.net.InetAddress
 * @see #getServerBindAddress()
 */
public String getServerBindAddressAsString() {
  try {
    if (getServerBindAddress() != null) {
      return getServerBindAddress().getCanonicalHostName();
    }

    final InetAddress localhost = SocketCreator.getLocalHost();

    return localhost.getCanonicalHostName();
  } catch (UnknownHostException ignore) {
    // TODO determine a better value for the host on which the Server is running to return here...
    // NOTE returning localhost/127.0.0.1 implies the serverBindAddress was null and no IP address
    // for localhost
    // could be found
    return "localhost/127.0.0.1";
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:32,代碼來源:ServerLauncher.java

示例4: getBindAddressAsString

import java.net.InetAddress; //導入方法依賴的package包/類
/**
 * Gets the host, as either hostname or IP address, on which the Locator was bound and running. An
 * attempt is made to get the canonical hostname for IP address to which the Locator was bound for
 * accepting client requests. If the bind address is null or localhost is unknown, then a default
 * String value of "localhost/127.0.0.1" is returned.
 * 
 * Note, this information is purely information and should not be used to re-construct state or
 * for other purposes.
 * 
 * @return the hostname or IP address of the host running the Locator, based on the bind-address,
 *         or 'localhost/127.0.0.1' if the bind address is null and localhost is unknown.
 * @see java.net.InetAddress
 * @see #getBindAddress()
 */
protected String getBindAddressAsString() {
  try {
    if (getBindAddress() != null) {
      return getBindAddress().getCanonicalHostName();
    }

    InetAddress localhost = SocketCreator.getLocalHost();

    return localhost.getCanonicalHostName();
  } catch (UnknownHostException ignore) {
    // TODO determine a better value for the host on which the Locator is running to return
    // here...
    // NOTE returning localhost/127.0.0.1 implies the bindAddress was null and no IP address for
    // localhost
    // could be found
    return "localhost/127.0.0.1";
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:33,代碼來源:LocatorLauncher.java

示例5: main

import java.net.InetAddress; //導入方法依賴的package包/類
public static void main(String[] args) throws UnknownHostException {
    List<Long> list=new ArrayList<>();
    list.add(66L);
    list.add(77L);
    String str=String.format("abc %s" ,list);
    System.out.println(str);

    InetAddress inetAddress = InetAddress.getLocalHost();
    String canonical = inetAddress.getCanonicalHostName();
    String host = inetAddress.getHostName();

    System.out.println(canonical);
    System.out.println(host);

}
 
開發者ID:ChenXun1989,項目名稱:ace,代碼行數:16,代碼來源:TestInteAddress.java

示例6: getRemoteHostName

import java.net.InetAddress; //導入方法依賴的package包/類
/**
 *  Gets the fully qualified domain name the requesting IP. Best effort method, meaning we may not be able to
 *  return the FQDN depending on the underlying system configuration. If not avaialable, the the IP address
 *  is returned instead.
 *
 * @return    The remote host domain name, for example mysite.org, or IP address if not available
 */
public String getRemoteHostName() {
	String t = doc.get("remotehost");

	if (t == null || t.trim().length() == 0)
		return null;
	try {
		InetAddress address = InetAddress.getByName(t);
		return address.getCanonicalHostName();
	} catch (Throwable e) {
		return t;
	}
}
 
開發者ID:NCAR,項目名稱:joai-project,代碼行數:20,代碼來源:WebLogReader.java

示例7: getHostSystemName

import java.net.InetAddress; //導入方法依賴的package包/類
/**
 * @return this machine's fully qualified hostname or "unknownHostName" if one cannot be found.
 */
private static String getHostSystemName() {
  String hostname = "unknownHostName";
  try {
    InetAddress addr = SocketCreator.getLocalHost();
    hostname = addr.getCanonicalHostName();
  } catch (UnknownHostException uhe) {
  }
  return hostname;
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:13,代碼來源:HostStatHelper.java

示例8: testContainerManagerInitialization

import java.net.InetAddress; //導入方法依賴的package包/類
@Test
public void testContainerManagerInitialization() throws IOException {

  containerManager.start();

  InetAddress localAddr = InetAddress.getLocalHost();
  String fqdn = localAddr.getCanonicalHostName();
  if (!localAddr.getHostAddress().equals(fqdn)) {
    // only check if fqdn is not same as ip
    // api returns ip in case of resolution failure
    Assert.assertEquals(fqdn, context.getNodeId().getHost());
  }

  // Just do a query for a non-existing container.
  boolean throwsException = false;
  try {
    List<ContainerId> containerIds = new ArrayList<ContainerId>();
    ContainerId id =createContainerId(0);
    containerIds.add(id);
    GetContainerStatusesRequest request =
        GetContainerStatusesRequest.newInstance(containerIds);
    GetContainerStatusesResponse response =
        containerManager.getContainerStatuses(request);
    if(response.getFailedRequests().containsKey(id)){
      throw response.getFailedRequests().get(id).deSerialize();
    }
  } catch (Throwable e) {
    throwsException = true;
  }
  Assert.assertTrue(throwsException);
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:32,代碼來源:TestContainerManager.java

示例9: qualifyHost

import java.net.InetAddress; //導入方法依賴的package包/類
static URL qualifyHost(URL url) {
  try {
    InetAddress a = InetAddress.getByName(url.getHost());
    String qualHost = a.getCanonicalHostName();
    URL q = new URL(url.getProtocol(), qualHost, url.getPort(), url.getFile());
    return q;
  } catch (IOException io) {
    return url;
  }
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:11,代碼來源:StreamUtil.java

示例10: getCanonicalHostName

import java.net.InetAddress; //導入方法依賴的package包/類
@VisibleForTesting
protected String getCanonicalHostName(InetAddress localAddress) {
    return localAddress.getCanonicalHostName();
}
 
開發者ID:philipwhiuk,項目名稱:q-mail,代碼行數:5,代碼來源:SmtpTransport.java

示例11: getLoopbackHostName

import java.net.InetAddress; //導入方法依賴的package包/類
private static String getLoopbackHostName()
{
    InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
    return loopbackAddress.getCanonicalHostName();
}
 
開發者ID:apache,項目名稱:directory-ldap-api,代碼行數:6,代碼來源:Network.java

示例12: setupClass

import java.net.InetAddress; //導入方法依賴的package包/類
@BeforeClass
public static void setupClass() throws UnknownHostException {
    InetAddress address = InetAddress.getLocalHost();
    HOST = address.getCanonicalHostName();
}
 
開發者ID:jotak,項目名稱:hawkular-java-toolbox,代碼行數:6,代碼來源:HawkularFactoryTest.java

示例13: getValidationResultOnlineResult

import java.net.InetAddress; //導入方法依賴的package包/類
@Override
public void getValidationResultOnlineResult(HashMap<String, Object> event, double result) {
    Integer sip = (Integer) event.get(AthenaIndexField.MATCH_IPV4_SRC);
    Integer dip = (Integer) event.get(AthenaIndexField.MATCH_IPV4_DST);
    byte[] sbytes = BigInteger.valueOf(sip).toByteArray();
    byte[] dbytes = BigInteger.valueOf(dip).toByteArray();
    InetAddress saddress = null;
    InetAddress daddress = null;
    try {
        saddress = InetAddress.getByAddress(sbytes);
        daddress = InetAddress.getByAddress(dbytes);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    String[] array;
    array = saddress.getHostAddress().split("\\.");

    int endHost = Integer.parseInt(array[3]);
    List<Integer> hostValue = clusterHistory.get(endHost);
    Integer hostTargetValue = hostValue.get((int) result);
    hostTargetValue += 1;
    hostValue.set((int) result, hostTargetValue);
    clusterHistory.set(endHost, hostValue);

    String outputs = "[Instance in " + localIP.getHostAddress() + "]" + "sIP=" + saddress.getHostAddress() + ";dIP=" + daddress.getCanonicalHostName() + ";loc=" + set.get(saddress.getHostAddress()) + ";"
            + "detection results = " + result;

    System.out.print("host." + endHost + "=>");
    for (int i = 0; i < 10; i++) {
        System.out.print(i + "(" + hostValue.get(i) + ")" + " ");
        if (i == 0 || i == 1 || i == 3) {
            if (hostValue.get(i) > 100) {
                System.out.println("Host " +saddress.getHostAddress() + " has been blocked (Malicious host!)");
                issueBlock(saddress.getHostAddress(), daddress.getHostAddress());
            }
        }
    }
    System.out.print("\n");

    log.info(outputs);
    System.out.println(outputs);
    //Detection results in here
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:45,代碼來源:Main.java

示例14: testIpAddressToHostNameMappings

import java.net.InetAddress; //導入方法依賴的package包/類
private static void testIpAddressToHostNameMappings(String hostsFileName)
        throws Exception {
    System.out.println(" TEST IP ADDRESS TO HOST MAPPINGS ");
    InetAddress testAddress;
    String retrievedHost;
    String expectedHost = "testHost.testDomain";

    byte[] testHostIpAddr = { 10, 2, 3, 4 };
    byte[] testHostIpAddr2 = { 10, 5, 6, 7 };
    byte[] testHostIpAddr3 = { 10, 8, 9, 10 };
    byte[] testHostIpAddr4 = { 10, 8, 9, 11 };

    // add comment to hosts file
    addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,
            false);
    addMappingToHostsFile("testHost.testDomain", "10.2.3.4", hostsFileName,
            true);

    testAddress = InetAddress.getByAddress(testHostIpAddr);
    System.out.println("*******   testAddress == " + testAddress);
    retrievedHost = testAddress.getHostName();
    if (!expectedHost.equals(retrievedHost)) {
        throw new RuntimeException(
                "retrieved host name not equal to expected host name");
    }

    addMappingToHostsFile("testHost.testDomain", "10.5.6.7", hostsFileName,
            true);

    testAddress = InetAddress.getByAddress(testHostIpAddr2);
    System.out.println("*******   testAddress == " + testAddress);
    retrievedHost = testAddress.getHostName();
    System.out.println("*******   retrievedHost == " + retrievedHost);
    if (!expectedHost.equals(retrievedHost)) {
        throw new RuntimeException("retrieved host name " + retrievedHost
                + " not equal to expected host name" + expectedHost);
    }

    testAddress = InetAddress.getByAddress(testHostIpAddr4);
    System.out.println("*******   testAddress == " + testAddress);
    if ("10.8.9.11".equalsIgnoreCase(testAddress.getCanonicalHostName())) {
        System.out.println("addr = " + addrToString(testHostIpAddr4)
                + "  resolve to a host address as expected");
    } else {
        System.out.println("addr = " + addrToString(testHostIpAddr4)
                + " does not resolve as expected, testAddress == " + testAddress.getCanonicalHostName());
        throw new RuntimeException("problem with resolving "
                + addrToString(testHostIpAddr4));
    }

    try {
        addMappingToHostsFile("", "10.8.9.10", hostsFileName, true);
        testAddress = InetAddress.getByAddress(testHostIpAddr3);
        System.out.println("*******   testAddress == " + testAddress);
        retrievedHost = testAddress.getCanonicalHostName();
    } catch (Throwable t) {
        throw new RuntimeException("problem with resolving "
                + addrToString(testHostIpAddr3));
    }

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:62,代碼來源:InternalNameServiceTest.java


注:本文中的java.net.InetAddress.getCanonicalHostName方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。