简体   繁体   English

为什么我无法通过浏览器连接到 localhost 上的 http 服务器?

[英]Why can't I connect to http server on localhost through a browser?

I wrote a very simple Java http server for exercising purposes.我写了一个非常简单的 Java http 服务器用于练习。 I test it with cURL and everything seems to work fine but when I try to send a request from a browser我用 cURL 测试它,一切似乎都正常,但是当我尝试从浏览器发送请求时

http://localhost:6666/

the server does not respond.服务器没有响应。 I even put a marking System.out.println() at the point when the server socket accepts a connection which doesn't seem to fire when i try to hit the server through a browser.我什至在服务器套接字接受连接时放置了一个标记 System.out.println(),当我尝试通过浏览器访问服务器时,该连接似乎没有触发。 Please help me out with this.这个你能帮我吗。 Thanks :)谢谢 :)

EDIT: Part of the code:编辑:部分代码:

public class Server {

    private ServerSocket serverSocket;
    private Socket socket;
    public Server() {
        try {
            serverSocket = new ServerSocket(6666);
            while (true) {
                socket = serverSocket.accept();
                System.out.println("Whoop! Connection!");
                Request request = new Request(socket);
                request.run();
            }
        } catch (IOException e) {
            e.printStackTrace();
          }
    }
}

Where Request is a class which extends Thread in order to handle multiple requests其中 Request 是一个扩展 Thread 以处理多个请求的类

(I'm assuming you are using exactly the same URL in the browser and using curl ...) (我假设您在浏览器中使用完全相同的 URL 并使用curl ...)

If the browser is running on a different host to the service, then the reason is that localhost IP addresses (eg 127.0.0.1 ) are not routed to any other hosts apart from the host they were sent from.如果浏览器在与服务不同的主机上运行,​​则原因是localhost IP 地址(例如127.0.0.1 )不会路由到除发送它们的主机之外的任何其他主机。 (That's what "local" means ...) In short, this is normal behaviour. (这就是“本地”的意思......)简而言之,这是正常的行为。 (And maybe you are running curl and the browser on different hosts.) (也许您正在不同的主机上运行curl和浏览器。)

If the browser is running on the same host as the service, this behaviour is a bit puzzling.如果浏览器与服务运行在同一台主机上,这种行为有点令人费解。 However there are some possible explanations:然而,有一些可能的解释:

  • You may have some strange network proxy settings in your browser.您的浏览器中可能有一些奇怪的网络代理设置。 For example, if you configure the browser to send ALL http requests (including 127.0.0.1) to an HTTP proxy on another machine, when the proxy relays the request to the real machine, it will go to the wrong place.例如,如果您将浏览器配置为将所有 http 请求(包括 127.0.0.1)发送到另一台机器上的 HTTP 代理,则当代理将请求中继到真机时,它会转到错误的地方。

  • The localhost domain name may be bound to some strange IP address; localhost域名可能绑定了一些奇怪的IP地址; eg something other than a 127.xxx IP address.例如,不是127.xxx IP 地址的东西。 (It is a strange thing to do, but I've heard of misguided people doing it.) (这是一件很奇怪的事情,但我听说过一些被误导的人这样做。)

  • The 127.0.0.1 IP address might have been bound to something other that the loopback network adapter. 127.0.0.1 IP 地址可能已绑定到环回网络适配器以外的其他内容。 (I don't know if this is technically possible ... ) (我不知道这在技术上是否可行......)

  • If you are using iptables to implement routing on a virtual network, you could be sending 127.0.0.1 packets to the wrong place.如果您使用 iptables 在虚拟网络上实现路由,您可能会将 127.0.0.1 数据包发送到错误的地方。 (I don't know if this is technically possible ... ) (我不知道这在技术上是否可行......)

The first bullet seems most likely to me.第一颗子弹在我看来最有可能。

Looks like port 6666 is considered unsafe by many browsers for security reasons.出于安全原因,许多浏览器认为端口 6666 是不安全的。 Please try some other ports may be port 3000 or 5000(I am just throwing a number here) it should work.请尝试一些其他端口可能是端口 3000 或 5000(我只是在这里抛出一个数字)它应该可以工作。

Browsers only understand http and some other protocols, such as ftp .浏览器只理解http和其他一些协议,比如ftp Your serverSocket does not implement any protocol.您的serverSocket没有实现任何协议。 If you want to see something, maybe you can try in console如果你想看一些东西,也许你可以在控制台中尝试

# telnet localhost 6666 

EDIT编辑

Here another question (and answer) for a correct implementation of an HTTP server: A Simple Http Server with Java/Socket?这是正确实现 HTTP 服务器的另一个问题(和答案): A Simple Http Server with Java/Socket?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM