当前位置: 首页>>代码示例>>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;未经允许,请勿转载。