本文整理汇总了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();
}
示例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();
}
}
示例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");
}
示例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));
}
示例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();
}
示例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();
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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();
}
}
示例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;
}
}
示例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;
}
示例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;
}
示例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;
}
}