我正在通过 com.sun.net.httpserver 设置 HttpServer。 我希望能够通过我的公共(public) ipv4 地址访问服务器。
我已经设法让它与localhost
一起工作。
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", Main::server.setExecutor(null);
server.start();
我尝试过
InetAddress ip = InetAddress.getByName("83.180.65.342");
HttpServer server = HttpServer.create(new InetSocketAddress(ip, 8080), 0);
但它返回
java.ne.BindException: Cannot assign requested address: bind
最佳答案
您是否在本地开发计算机上执行此操作,而您的公共(public)计算机实际上是其他地方的不同计算机?这显然行不通。
如果这是问题所在,请将您的 IP 地址设置为安装中的可配置属性,而不是对其进行硬编码。或者使用 getLocalHost()(并正确配置计算机的网络和 DNS 选项,这样它就不会返回 127.0.0.1)。
关于java - 使用公共(public) ipv4 地址创建 HttpServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038623/