本文整理汇总了Java中java.net.InetAddress.getHostName方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getHostName方法的具体用法?Java InetAddress.getHostName怎么用?Java InetAddress.getHostName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.getHostName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: SocksSocket
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Connects to given ip and port using given Proxy server.
*
* @param p
* Proxy to use.
* @param ip
* Machine to connect to.
* @param port
* Port to which to connect.
*/
public SocksSocket(SocksProxyBase p, InetAddress ip, int port)
throws SocksException {
if (p == null) {
throw new SocksException(SocksProxyBase.SOCKS_NO_PROXY);
}
this.proxy = p.copy();
this.remoteIP = ip;
this.remotePort = port;
this.remoteHost = ip.getHostName();
if (proxy.isDirect(remoteIP)) {
doDirect();
} else {
processReply(proxy.connect(ip, port));
}
}
示例2: connect
import java.net.InetAddress; //导入方法依赖的package包/类
void connect(InetAddress address, int port) {
if(this.address == null)
return;
try {
// First, close the previous connection if any.
cleanUp();
stw = new SyslogTcpWriter(new Socket(address, port).getOutputStream(), syslogFacility);
} catch(IOException e) {
if (e instanceof InterruptedIOException) {
Thread.currentThread().interrupt();
}
String msg = "Could not connect to remote log4j server at ["
+address.getHostName()+"].";
if(reconnectionDelay > 0) {
msg += " We will try again later.";
fireConnector(); // fire the connector thread
} else {
msg += " We are not retrying.";
errorHandler.error(msg, e, ErrorCode.GENERIC_FAILURE);
}
LogLog.error(msg);
}
}
示例3: testRunStarted
import java.net.InetAddress; //导入方法依赖的package包/类
/**
*
* @see org.junit.runner.notification.RunListener#testRunStarted(org.junit.runner.Description)
*/
@Override
public void testRunStarted( Description description ) throws Exception {
if (log.isDebugEnabled()) { // currently always returns null for Description parameter
log.debug("testRunStarted(): Called before any test is run. Description of all tests expected: "
+ description);
}
String runNameSysProp = AtsSystemProperties.getPropertyAsString(AtsSystemProperties.TEST_HARNESS__JUNIT_RUN_NAME,
"JUnit run(nameless)");
String hostNameIp = "";
try {
InetAddress addr = InetAddress.getLocalHost();
hostNameIp = addr.getHostName() + "/" + addr.getHostAddress();
} catch (UnknownHostException uhe) {
hostNameIp = null;
}
logger.startRun(runNameSysProp /* no suite name in JUnit */, CommonConfigurator.getInstance().getOsName(),
CommonConfigurator.getInstance().getProductName(),
CommonConfigurator.getInstance().getVersionName(),
CommonConfigurator.getInstance().getBuildName(), hostNameIp);
super.testRunStarted(description);
}
示例4: getHostName
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getHostName(String address) {
try {
int i = address.indexOf(':');
if (i > -1) {
address = address.substring(0, i);
}
String hostname = hostNameCache.get(address);
if (hostname != null && hostname.length() > 0) {
return hostname;
}
InetAddress inetAddress = InetAddress.getByName(address);
if (inetAddress != null) {
hostname = inetAddress.getHostName();
hostNameCache.put(address, hostname);
return hostname;
}
} catch (Throwable e) {
// ignore
}
return address;
}
示例5: SyslogTcpAppender
import java.net.InetAddress; //导入方法依赖的package包/类
/**
Connects to remote server at <code>address</code> and <code>port</code>.
*/
public SyslogTcpAppender(InetAddress address, int port, int syslogFacility) {
this.address = address;
this.remoteHost = address.getHostName();
this.port = port;
this.syslogFacility = syslogFacility;
this.initSyslogFacilityStr();
connect(address, port);
}
示例6: Socks5Message
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Construct client request or server response.
*
* @param cmd
* - Request/Response code.
* @param ip
* - IP field.
* @paarm port - port field.
*/
public Socks5Message(int cmd, InetAddress ip, int port) {
super(cmd, ip, port);
if (ip == null) {
this.host = "0.0.0.0";
} else {
this.host = ip.getHostName();
}
this.version = SOCKS_VERSION;
byte[] addr;
if (ip == null) {
addr = new byte[4];
addr[0] = addr[1] = addr[2] = addr[3] = 0;
} else {
addr = ip.getAddress();
}
if (addr.length == 4) {
addrType = SOCKS_ATYP_IPV4;
} else {
addrType = SOCKS_ATYP_IPV6;
}
data = new byte[6 + addr.length];
data[0] = (byte) SOCKS_VERSION; // Version
data[1] = (byte) command; // Command
data[2] = (byte) 0; // Reserved byte
data[3] = (byte) addrType; // Address type
// Put Address
System.arraycopy(addr, 0, data, 4, addr.length);
// Put port
data[data.length - 2] = (byte) (port >> 8);
data[data.length - 1] = (byte) (port);
}
示例7: getHostnameOrAddress
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getHostnameOrAddress() {
try {
final InetAddress addr = InetAddress.getLocalHost();
return addr.getHostName();
} catch (UnknownHostException e) {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces == null) {
return "";
}
NetworkInterface intf = interfaces.nextElement();
Enumeration<InetAddress> addresses = intf.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (address instanceof Inet4Address) {
return address.getHostAddress();
}
}
interfaces = NetworkInterface.getNetworkInterfaces();
while (addresses.hasMoreElements()) {
return addresses.nextElement().getHostAddress();
}
return "";
} catch (SocketException e1) {
return "";
}
}
}
示例8: getHostname
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns the name of the local host.
*
* @return The host name
* @throws UnknownHostException if IP address of a host could not be determined
*/
public static String getHostname() throws UnknownHostException {
InetAddress inetAddress = InetAddress.getLocalHost();
String hostName = inetAddress.getHostName();
assertTrue((hostName != null && !hostName.isEmpty()),
"Cannot get hostname");
return hostName;
}
示例9: umnt
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public XDR umnt(XDR xdr, XDR out, int xid, InetAddress client) {
String path = xdr.readString();
if (LOG.isDebugEnabled()) {
LOG.debug("MOUNT UMNT path: " + path + " client: " + client);
}
String host = client.getHostName();
mounts.remove(new MountEntry(host, path));
RpcAcceptedReply.getAcceptInstance(xid, new VerifierNone()).write(out);
return out;
}
示例10: isHostAuthenticatedMember
import java.net.InetAddress; //导入方法依赖的package包/类
public boolean isHostAuthenticatedMember ( String ipAddress ) {
try {
InetAddress addr = InetAddress.getByName( ipAddress );
String remoteServerName = addr.getHostName();
if ( remoteServerName.equals( "127.0.0.1" ) ) {
remoteServerName = "localhost";
}
if ( remoteServerName.contains( "." ) ) {
remoteServerName = remoteServerName.substring( 0, remoteServerName.indexOf( "." ) );
}
if ( remoteServerName.equals( "rtp-someDeveloper-8811" ) && getCurrentLifeCycle().equals( "dev" ) ) {
logger.warn( "DEVELOPER TESTING: Resolved: {} to host: {}", ipAddress, remoteServerName );
return true;
}
logger.debug( "Resolved: {} to host: {}", ipAddress, remoteServerName );
return getAllHostsInAllPackagesInCurrentLifecycle().contains( remoteServerName );
} catch (Exception e) {
logger.error( "Failed to get hostname" );
}
return false;
}
示例11: addNewPeer
import java.net.InetAddress; //导入方法依赖的package包/类
public void addNewPeer(Socket pSocket) {
IllegalArgument.ifNull("Socket", pSocket);
InetAddress theirAddress = pSocket.getInetAddress();
String theirName = theirAddress.getHostName();
String theirIP = theirAddress.getHostAddress();
String name = theirIP.equals(theirName) ? null : "(Host: " + theirName + ")";
zPendingPeers.add(pSocket, new PeerInfo(name, theirIP));
}
示例12: process
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
Args.notNull(request, "HTTP request");
final HttpCoreContext corecontext = HttpCoreContext.adapt(context);
final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
final String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
}
if (!request.containsHeader(HTTP.TARGET_HOST)) {
HttpHost targethost = corecontext.getTargetHost();
if (targethost == null) {
final HttpConnection conn = corecontext.getConnection();
if (conn instanceof HttpInetConnection) {
// Populate the context with a default HTTP host based on the
// inet address of the target host
final InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
final int port = ((HttpInetConnection) conn).getRemotePort();
if (address != null) {
targethost = new HttpHost(address.getHostName(), port);
}
}
if (targethost == null) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
} else {
throw new ProtocolException("Target host missing");
}
}
}
request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
}
}
示例13: localHostName
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns the host name for the local host. If the operation is not allowed
* by the security check, the textual representation of the IP address of
* the local host is returned instead. If the ip address of the local host
* cannot be resolved or if there is any other failure, "localhost" is
* returned as a fallback.
*/
public static String localHostName() {
try {
InetAddress localhost = InetAddress.getLocalHost();
return localhost.getHostName();
} catch (Exception e) {
InternalLogFactory.getLog(AwsHostNameUtils.class)
.debug(
"Failed to determine the local hostname; fall back to "
+ "use \"localhost\".", e);
return "localhost";
}
}
示例14: updateApplicationVariables
import java.net.InetAddress; //导入方法依赖的package包/类
private void updateApplicationVariables ()
throws UnknownHostException {
httpdIntegration.updateConstants();
WAR_DIR = STAGING + CSAP_SERVICE_PACKAGES;
BUILD_DIR = STAGING + "/build/";
// Use relative paths to handle all use cases
CSAP_DEFINITION_FOLDER_FOR_JUNITs = getDefinitionFile()
.getParentFile()
.getAbsolutePath();
InetAddress addr = InetAddress.getLocalHost();
// Get hostname
HOST_NAME = addr.getHostName();
if ( HOST_NAME.indexOf( "." ) != -1 ) {
// in case of host.somecompany.com, strip off domain
HOST_NAME = HOST_NAME.substring( 0, HOST_NAME.indexOf( "." ) );
}
// hook for testing on eclipse
logger.info( "Host name is: {}", HOST_NAME );
if ( isRunningOnDesktop() ) {
HOST_NAME = "localhost";
//
logger.info( "\n\n Did not find STAGING env var, forcing test mode - setting host to localhost \n\n" );
}
if ( isJvmInManagerMode() ) {
refreshConfigHandleOnlyOnMgrs = refreshConfigPool.scheduleWithFixedDelay(
new Runnable() {
@Override
public void run () {
updateCache( false );
}
}, 10, 10, TimeUnit.SECONDS );
}
}
示例15: main
import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
InetAddress localAddress = InetAddress.getLocalHost();
String[] hostlist = new String[] {
localAddress.getHostAddress(), localAddress.getHostName() };
for (int i = 0; i < hostlist.length; i++) {
System.setProperty("java.rmi.server.hostname", hostlist[i]);
Remote impl = new ChangeHostName();
System.err.println("\ncreated impl extending URO: " + impl);
Receiver stub = (Receiver) RemoteObject.toStub(impl);
System.err.println("stub for impl: " + stub);
System.err.println("invoking method on stub");
stub.receive(stub);
UnicastRemoteObject.unexportObject(impl, true);
System.err.println("unexported impl");
if (stub.toString().indexOf(hostlist[i]) >= 0) {
System.err.println("stub's ref contains hostname: " +
hostlist[i]);
} else {
throw new RuntimeException(
"TEST FAILED: stub's ref doesn't contain hostname: " +
hostlist[i]);
}
}
System.err.println("TEST PASSED");
}