当前位置: 首页>>代码示例>>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;未经允许,请勿转载。