本文整理汇总了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;
}
示例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);
}
}
示例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";
}
}
示例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";
}
}
示例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);
}
示例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;
}
}
示例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;
}
示例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);
}
示例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;
}
}
示例10: getCanonicalHostName
import java.net.InetAddress; //导入方法依赖的package包/类
@VisibleForTesting
protected String getCanonicalHostName(InetAddress localAddress) {
return localAddress.getCanonicalHostName();
}
示例11: getLoopbackHostName
import java.net.InetAddress; //导入方法依赖的package包/类
private static String getLoopbackHostName()
{
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
return loopbackAddress.getCanonicalHostName();
}
示例12: setupClass
import java.net.InetAddress; //导入方法依赖的package包/类
@BeforeClass
public static void setupClass() throws UnknownHostException {
InetAddress address = InetAddress.getLocalHost();
HOST = address.getCanonicalHostName();
}
示例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
}
示例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));
}
}