做网站优化推广,东莞市建设规划局网站首页,wordpress婚礼主题,北海手机网站建设某人想在h小时内钓到在进行测试自动化时#xff0c;通常需要知道当前计算机的名称#xff0c;以提示另一台计算机连接到它#xff0c;特别是在并行运行测试的情况下。 本周#xff0c;我试图对服务器进行测试#xff0c;以使其连接回在从属测试计算机上运行的WireMock服务… 某人想在h小时内钓到 在进行测试自动化时通常需要知道当前计算机的名称以提示另一台计算机连接到它特别是在并行运行测试的情况下。 本周我试图对服务器进行测试以使其连接回在从属测试计算机上运行的WireMock服务器。 堆栈溢出的标准响应是使用以下模式获取网络地址。 在我的版本中如果我们无法解析名称则假设我们正在VPN上的开发人员便携式计算机上运行因此所有测试都在同一台计算机上运行。 因此localhost String hostName localhost;
try {InetAddress addr InetAddress.getLocalHost();String suggestedName addr.getCanonicalHostName();// Rough test for IP address, if IP address assume a local lookup// on VPNif (!suggestedName.matches((\\d{1,3}\\.?){4}) !suggestedName.contains(:)) {hostName suggestedName;}
} catch (UnknownHostException ex) {
}System.out.println(hostName); 问题来了我们必须信任本地计算机设置例如/ etc / hostname这可能导致无法从另一台计算机访问的网络名称。 为了解决这个问题我编写了以下代码以在可用的网络接口上工作以找到可用于与本机对话的可远程寻址的网络地址名称。 我可以使用IP地址但是很难记住它们尤其是当我们向IPv6迈进时 String hostName stream(wrap(NetworkInterface::getNetworkInterfaces).get())// Only alllow interfaces that are functioning.filter(wrap(NetworkInterface::isUp))// Flat map to any bound addresses.flatMap(n - stream(n.getInetAddresses()))// Fiter out any local addresses.filter(ia - !ia.isAnyLocalAddress() !ia.isLinkLocalAddress() !ia.isLoopbackAddress())// Map to a name.map(InetAddress::getCanonicalHostName)// Ignore if we just got an IP back.filter(suggestedName - !suggestedName.matches((\\d{1,3}\\.?){4}) !suggestedName.contains(:)).findFirst()// In my case default to localhost.orElse(localhost);System.out.println(hostName); 您可能会注意到那里使用了一些支持方法来整理代码如果您感兴趣的话这里是必需的支持方法。 FunctionalInterface
public interface ThrowingPredicateT, E extends Exception{boolean test(T t) throws E;
}FunctionalInterface
public interface ThrowingSupplierT, E extends Exception{T get() throws E;
}public static T, E extends Exception PredicateT wrap(ThrowingPredicateT, E th) {return t - {try {return th.test(t);} catch (Exception ex) {throw new RuntimeException(ex);}};
}public static T, E extends Exception SupplierT wrap(ThrowingSupplierT, E th) {return () - {try {return th.get();} catch (Exception ex) {throw new RuntimeException(ex);}};
}// http://stackoverflow.com/a/23276455
public static T StreamT stream(EnumerationT e) {return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new IteratorT() {public T next() {return e.nextElement();}public boolean hasNext() {return e.hasMoreElements();}},Spliterator.ORDERED), false);
}翻译自: https://www.javacodegeeks.com/2016/06/getting-name-someone-connect-back-server.html某人想在h小时内钓到