无法从外部客户端连接到 Java Server
我正在创建一个个人工具,通过 Java 中的 HTTP 提供文件服务。 对于 HTTP 服务器部分,我使用 com.sun.net.httpserver.HttpServer 包。 当我使用本地主机或外部主机名从自己的电脑连接时,它可以工作,但是当我尝试从外部客户端连接到它时,它就不能工作。 我还想提一下,java 默认策略文件从未被修改过,而且我位于路由器后面,并且端口已正确转发。 我尝试修改策略文件,但没有取得任何成功,尽管我不知道设置是否正确或是否已应用,并且我读到了一个存在类似问题的线程,但使用了 rmi已通过设置 java.rmi.server.hostname 系统属性解决,但我认为这在我的情况下也可用,因为服务器使用 TCP。
这是初始化服务器的代码:
Settings settings = Controller.getInstance().getModel().getSettings();
int port = settings.getHTTP_PORT();
server = HttpServer.create(new InetSocketAddress(port), 20);
String cachepath = settings.getCachepath();
if(!cachepath.startsWith("/")) cachepath = "/"+cachepath;
server.createContext(cachepath, new HTTPServerHandler());
server.start();
如果你们中的任何人都可以指出我可以做什么来解决这个问题,我将非常感激。
I'm creating a personal tool that serves files through HTTP in Java.
For the HTTP Server part I'm using the com.sun.net.httpserver.HttpServer package.
When I connect from my own PC using either localhost or the external hostname, it works, but when I try to connect from an external client to it, it just doesn't.
I'd also like to mention that the java default policies file hasn't ever been modified and that I'm behind a router, and that the port is properly forwarded.
I tried modifying the policies file but I didn't have any success, although I don't know if the settings were correct or if they got applied, and I read a thread in which there was a similar problem, but using rmi which has been solved by setting the java.rmi.server.hostname system property, but I don't think this is also available in my case because the server uses TCP.
Here's the code that initializes the server:
Settings settings = Controller.getInstance().getModel().getSettings();
int port = settings.getHTTP_PORT();
server = HttpServer.create(new InetSocketAddress(port), 20);
String cachepath = settings.getCachepath();
if(!cachepath.startsWith("/")) cachepath = "/"+cachepath;
server.createContext(cachepath, new HTTPServerHandler());
server.start();
If any of you guys can point to what I can do in order to solve this I would really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来我尝试连接的服务器是套接字连接被阻止的唯一情况。我请其他人连接到它并且它起作用了。
It seems the server I was trying to connect to was the only case the socket connections were blocked. I asked someone else to connect to it and it worked.