当前位置: 首页>>代码示例>>Java>>正文


Java InetAddress.getLocalHost方法代码示例

本文整理汇总了Java中java.net.InetAddress.getLocalHost方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getLocalHost方法的具体用法?Java InetAddress.getLocalHost怎么用?Java InetAddress.getLocalHost使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.InetAddress的用法示例。


在下文中一共展示了InetAddress.getLocalHost方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: newSelf

import java.net.InetAddress; //导入方法依赖的package包/类
private void newSelf(File file, ApplicationSingletonListener listener) throws IOException {
    if (file.exists())
        file.delete();
    DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
    Math.random();
    Math.random();
    Math.random();
    double temp = Math.random();
    int password = (int) (32000 * temp);

    out.writeInt(password);
    out.writeInt(port);
    out.close();

    server = new ApplicationServer(password, new ServerSocket(port, 1, InetAddress
            .getLocalHost()), listener);
    server.start();
}
 
开发者ID:addertheblack,项目名称:myster,代码行数:19,代码来源:ApplicationSingleton.java

示例2: test

import java.net.InetAddress; //导入方法依赖的package包/类
private static void test() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    InetAddress localHost = InetAddress.getLocalHost();
    dc.bind(new InetSocketAddress(localHost, 0));

    Server server = new Server(dc.getLocalAddress());
    Thread serverThread = new Thread(server);
    serverThread.start();

    try {
        InetSocketAddress isa = new InetSocketAddress(localHost, server.port());
        dc.connect(isa);

        ByteBuffer bb = ByteBuffer.allocateDirect(12);
        bb.order(ByteOrder.BIG_ENDIAN);
        bb.putInt(1).putLong(1);
        bb.flip();

        dc.write(bb);
        bb.rewind();
        dc.write(bb);
        bb.rewind();
        dc.write(bb);

        Thread.sleep(2000);

        serverThread.interrupt();
        server.throwException();
    } finally {
        dc.close();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:EmptyBuffer.java

示例3: connectToManagementApi

import java.net.InetAddress; //导入方法依赖的package包/类
private Socket connectToManagementApi(final long timeout, final TimeUnit timeUnit)
    throws Exception {
  final long actualTimeout = System.currentTimeMillis() + timeUnit.toMillis(timeout);

  final InetAddress localhost = InetAddress.getLocalHost();
  while (System.currentTimeMillis() < actualTimeout) {
    try {
      final Socket socket = new Socket();
      socket.connect(new InetSocketAddress(localhost, 12345), 500);
      return socket;
    } catch (final Exception ex) {
      // Nevermind, wait and try again
      Thread.sleep(100L);
    }
  }

  throw new TimeoutException("Cannot connect to TestApp's management interface");
}
 
开发者ID:SAP,项目名称:java-memory-assistant,代码行数:19,代码来源:ProcessBuilder.java

示例4: testEquals

import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void testEquals() throws UnknownHostException {
  InetAddress address = InetAddress.getLocalHost();
  DistributionLocatorId dLI1 = new DistributionLocatorId(address, 40404, "127.0.0.1", null);
  DistributionLocatorId dLI2 = dLI1;
  @SuppressWarnings("RedundantStringConstructorCall")
  DistributionLocatorId dLI3 =
      new DistributionLocatorId(address, 40404, new String("127.0.0.1"), null);
  @SuppressWarnings("RedundantStringConstructorCall")
  DistributionLocatorId dLI4 = new DistributionLocatorId(InetAddress.getByName("localhost"),
      50505, new String("128.0.0.1"), null);

  assertTrue(dLI1.equals(dLI2));
  assertTrue(dLI1.equals(dLI3));
  assertFalse(dLI1.equals(dLI4));

}
 
开发者ID:ampool,项目名称:monarch,代码行数:18,代码来源:DistributionLocatorIdJUnitTest.java

示例5: simpleRequestToShortestPath

import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void simpleRequestToShortestPath() throws Exception {
    CoapServer cnn = CoapServer.builder().transport(0).build();
    cnn.start();

    CoapPacket request = new CoapPacket(new InetSocketAddress(InetAddress.getLocalHost(), SERVER_PORT));
    request.setMethod(Method.GET);
    request.headers().setUriPath("/");
    request.setMessageId(1648);

    assertEquals("Shortest path", cnn.makeRequest(request).join().getPayloadString());
    cnn.stop();
}
 
开发者ID:ARMmbed,项目名称:java-coap,代码行数:14,代码来源:ClientServerTest.java

示例6: getIP

import java.net.InetAddress; //导入方法依赖的package包/类
public static String getIP() {
	InetAddress ip = null;
	try {
		ip = InetAddress.getLocalHost();
	} catch (UnknownHostException e) {
		e.printStackTrace();
	}
	return ip.getHostName() + "@" + ip.getHostAddress();
}
 
开发者ID:xiaojieliu7,项目名称:MicroServiceProject,代码行数:10,代码来源:Systems.java

示例7: getRemote

import java.net.InetAddress; //导入方法依赖的package包/类
/**
 * 返回远程地址,包括协议。例如: 192.168.12.10<br>
 * 检查是否有转发,并x_forwarded_for的情况下,<br>
 * 有 客户端和服务器之间的代理。
 * 
 * @return string url value
 */
private String getRemote(HttpServletRequest request) {
	if (null == request)
		return "";
	String ip = request.getHeader("X-Requested-For");
	if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("X-Forwarded-For");
	}
	if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("Proxy-Client-IP");
	}
	if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("WL-Proxy-Client-IP");
	}
	if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("HTTP_CLIENT_IP");
	}
	if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("HTTP_X_FORWARDED_FOR");
	}
	if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getRemoteAddr();
		if ("127.0.0.1".equals(ip) || "localhost".equals(ip.toLowerCase())) {
			try {
				// 根据网卡取本机配置的IP
				InetAddress inet = InetAddress.getLocalHost();
				ip = inet.getHostAddress();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
	}
	// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
	if (ip != null && ip.length() > 15) {
		if (ip.indexOf(",") > 0) {
			ip = ip.substring(0, ip.indexOf(","));
		}
	}
	return ip;
}
 
开发者ID:jiangzongyao,项目名称:kettle_support_kettle8.0,代码行数:47,代码来源:Router.java

示例8: setLocalAddr

import java.net.InetAddress; //导入方法依赖的package包/类
protected void setLocalAddr() throws UnknownHostException {
  InetAddress ip = InetAddress.getLocalHost();
  if (ip != null) {
    String submitHostAddress = ip.getHostAddress();
    String submitHostName = ip.getHostName();
    conf.set(AngelConf.JOB_SUBMITHOST, submitHostName);
    conf.set(AngelConf.JOB_SUBMITHOSTADDR, submitHostAddress);
  }
}
 
开发者ID:Tencent,项目名称:angel,代码行数:10,代码来源:AngelClient.java

示例9: ErrorLoggerFacadeImpl

import java.net.InetAddress; //导入方法依赖的package包/类
public ErrorLoggerFacadeImpl() {
    try {
        InetAddress ipAddr = InetAddress.getLocalHost();
        serverIpAddress = ipAddr.getHostAddress();
    } catch (Exception e) {
        LOG.error("Could not get IP address of server", e);
    }
}
 
开发者ID:Atypon-OpenSource,项目名称:wayf-cloud,代码行数:9,代码来源:ErrorLoggerFacadeImpl.java

示例10: launchWithSocketChannel

import java.net.InetAddress; //导入方法依赖的package包/类
public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(0));
    InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
                                                  ssc.socket().getLocalPort());
    SocketChannel sc1 = SocketChannel.open(isa);
    SocketChannel sc2 = ssc.accept();
    launch(className, options, args, Util.getFD(sc2));
    sc2.close();
    ssc.close();
    return sc1;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:Launcher.java

示例11: init

import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public void init()
{
                   System.setProperty("java.net.preferIPv4Stack", "true");
                   try{
                ip = InetAddress.getLocalHost();
                ipadd = ip.getHostAddress();
          }catch (UnknownHostException e)
	{
	       e.printStackTrace();
	}
}
 
开发者ID:Tejas07PSK,项目名称:EBwn2_ForOpenShift,代码行数:13,代码来源:AndroServlet.java

示例12: getHostAddress

import java.net.InetAddress; //导入方法依赖的package包/类
@SuppressForbidden(reason = "using localhost for logging on which host it is is fine")
private static InetAddress getHostAddress() {
    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        return null;
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:9,代码来源:Loggers.java

示例13: getHostName

import java.net.InetAddress; //导入方法依赖的package包/类
private static String getHostName() {
	InetAddress localHost = null;
	try {
		localHost = InetAddress.getLocalHost();
	} catch (UnknownHostException e) {
		logger.error("Error while getting host name", e);
	}
	return localHost != null ? localHost.getHostName() : null;
}
 
开发者ID:gagoyal01,项目名称:mongodb-rdbms-sync,代码行数:10,代码来源:Mailer.java

示例14: getIpAddr

import java.net.InetAddress; //导入方法依赖的package包/类
/**
 * 获取计算机的ip地址
 * @return
 */
public static String getIpAddr() {
	String ipAddress = null;
	try {
		InetAddress address = InetAddress.getLocalHost();
		ipAddress = address.getHostAddress();
	} catch (UnknownHostException e) {
		logger.error(e.getMessage(), e);
	}
	return ipAddress;
}
 
开发者ID:babymm,项目名称:mumu,代码行数:15,代码来源:WebUtil.java

示例15: isLocalhost

import java.net.InetAddress; //导入方法依赖的package包/类
/** @return true if someHost refers to localhost.
 */
public static boolean isLocalhost(String someHost) {
  if (null == someHost) {
    return false;
  }

  try {
    InetAddress localHostAddr = InetAddress.getLocalHost();
    InetAddress someAddr = InetAddress.getByName(someHost);

    return localHostAddr.equals(someAddr);
  } catch (UnknownHostException uhe) {
    return false;
  }
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:17,代码来源:DirectImportUtils.java


注:本文中的java.net.InetAddress.getLocalHost方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。