當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpServer.getAddress方法代碼示例

本文整理匯總了Java中com.sun.net.httpserver.HttpServer.getAddress方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServer.getAddress方法的具體用法?Java HttpServer.getAddress怎麽用?Java HttpServer.getAddress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.sun.net.httpserver.HttpServer的用法示例。


在下文中一共展示了HttpServer.getAddress方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
void test(String[] args) throws Exception {
    HttpServer server = startHttpServer();
    try {
        InetSocketAddress address = server.getAddress();
        URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress()
                          + ":" + address.getPort() + "/foo");
        doClient(uri);
    } finally {
        server.stop(0);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:12,代碼來源:UnmodifiableMaps.java

示例2: test

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
void test(String[] args) throws Exception {
    HttpServer server = startHttpServer();
    CookieHandler previousHandler = CookieHandler.getDefault();
    try {
        InetSocketAddress address = server.getAddress();
        URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress()
                          + ":" + address.getPort() + URI_PATH);
        populateCookieStore(uri);
        doClient(uri);
    } finally {
        CookieHandler.setDefault(previousHandler);
        server.stop(0);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:15,代碼來源:HttpOnly.java

示例3: main

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/test/InfiniteLoop", new RespHandler());
    server.start();
    try {
        InetSocketAddress address = server.getAddress();
        URL url = new URL("http://localhost:" + address.getPort()
                          + "/test/InfiniteLoop");
        final Phaser phaser = new Phaser(2);
        for (int i=0; i<10; i++) {
            HttpURLConnection uc = (HttpURLConnection)url.openConnection();
            final InputStream is = uc.getInputStream();
            final Thread thread = new Thread() {
                public void run() {
                    try {
                        phaser.arriveAndAwaitAdvance();
                        while (is.read() != -1)
                            Thread.sleep(50);
                    } catch (Exception x) { x.printStackTrace(); }
                }};
            thread.start();
            phaser.arriveAndAwaitAdvance();
            is.close();
            System.out.println("returned from close");
            thread.join();
        }
    } finally {
        server.stop(0);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:31,代碼來源:InfiniteLoop.java

示例4: create

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
static <T extends HttpServer> T create(HttpProtocolType protocol)
        throws IOException {
    final int max = addresses.size() + MAX;
    final List<HttpServer> toClose = new ArrayList<>();
    try {
        for (int i = 1; i <= max; i++) {
            HttpServer server = newHttpServer(protocol);
            server.bind(new InetSocketAddress("127.0.0.1", 0), 0);
            InetSocketAddress address = server.getAddress();
            String key = address.toString();
            if (addresses.addIfAbsent(key)) {
               System.out.println("Server bound to: " + key
                                  + " after " + i + " attempt(s)");
               return (T) server;
            }
            System.out.println("warning: address " + key
                               + " already used. Retrying bind.");
            // keep the port bound until we get a port that we haven't
            // used already
            toClose.add(server);
        }
    } finally {
        // if we had to retry, then close the servers we're not
        // going to use.
        for (HttpServer s : toClose) {
          try { s.stop(1); } catch (Exception x) { /* ignore */ }
        }
    }
    throw new IOException("Couldn't bind servers after " + max + " attempts: "
                          + "addresses used before: " + addresses);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:32,代碼來源:HTTPTestServer.java

示例5: test

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
void test(String[] args) throws Exception {
    HttpServer server = startHttpServer();
    try {
        InetSocketAddress address = server.getAddress();
        URI uri = new URI("http://localhost:" + address.getPort() + "/foo");
        doClient(uri);
    } finally {
        server.stop(0);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:UnmodifiableMaps.java


注:本文中的com.sun.net.httpserver.HttpServer.getAddress方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。