某人想在h小时内钓到_为某人命名以重新连接到您的服务器
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                某人想在h小时内钓到_为某人命名以重新连接到您的服务器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                某人想在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 ThrowingPredicate<T, E extends Exception>{boolean test(T t) throws E; }@FunctionalInterface public interface ThrowingSupplier<T, E extends Exception>{T get() throws E; }public static <T, E extends Exception> Predicate<T> wrap(ThrowingPredicate<T, E> th) {return t -> {try {return th.test(t);} catch (Exception ex) {throw new RuntimeException(ex);}}; }public static <T, E extends Exception> Supplier<T> wrap(ThrowingSupplier<T, E> th) {return () -> {try {return th.get();} catch (Exception ex) {throw new RuntimeException(ex);}}; }// http://stackoverflow.com/a/23276455 public static <T> Stream<T> stream(Enumeration<T> e) {return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<T>() {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小時內釣到
總結
以上是生活随笔為你收集整理的某人想在h小时内钓到_为某人命名以重新连接到您的服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: linux温度命令(linux温度)
- 下一篇: java8并行流_Java 8:Comp
