Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用公共ipv4地址创建HttpServer_Java_Httpserver_Ipv4 - Fatal编程技术网

Java 使用公共ipv4地址创建HttpServer

Java 使用公共ipv4地址创建HttpServer,java,httpserver,ipv4,Java,Httpserver,Ipv4,我正在通过com.sun.net.HttpServer设置一个HttpServer。 我希望能够通过我的公共ipv4地址访问服务器 我已经设法让它与localhost一起工作 HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); server.createContext("/", Main::server.setExecutor(null); server.start(); 我试过了 InetAddress

我正在通过com.sun.net.HttpServer设置一个HttpServer。 我希望能够通过我的公共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

您是否在本地开发机器上执行此操作,而您的公共机器实际上是其他地方的独立机器?这显然行不通


如果这是问题所在,请将IP地址设置为安装中的可配置属性,而不是硬编码。或者使用getLocalHost()(并正确配置计算机的网络和DNS选项,使其不会返回127.0.0.1)。

该端口上是否有其他内容正在侦听?@Steve Smith否,即使我尝试其他端口,它也会提供相同的输出您确定这是正确的IP吗?为什么不使用实际工作的
InetAddress.getLocalHost()
?@matanper。我想我必须使用我的公共ipv4地址,但看起来不像。非常感谢。