简体   繁体   English

为什么拒绝连接到localhost?

[英]Why connection to localhost is refused?

I have a server, to which a client machine connects. 我有一台服务器,客户端计算机连接到该服务器。 Recently I decided to encrypt the connection with stunnel, so now client program connects not directly to the server, but to localhost:8045 (I checked, and this port is not occupied). 最近,我决定用stunnel加密连接,因此现在客户端程序不直接连接到服务器,而是直接连接到localhost:8045(我检查了一下,这个端口没有被占用)。

Java code: Java代码:

URL url = new URL("http://localhost:8045/malibu/GetProviders");
InputStream stream = url.openStream();

And I get the following: 我得到以下信息:

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at java.net.Socket.<init>(Socket.java:366)
    at java.net.Socket.<init>(Socket.java:180)
    . . .

If I try to request the same page using curl , everything is fine. 如果我尝试使用curl请求相同的页面,一切都很好。

What can cause such behavior? 什么会导致这种行为?

EDIT: Yes, there is a listening socket - running netstat -avn | grep 8045 编辑:是的,有一个侦听套接字-运行netstat -avn | grep 8045 netstat -avn | grep 8045 gives: netstat -avn | grep 8045提供:

tcp6       0      0 ::1:8045                :::*                    LISTEN

The listening socket is bound to the IPv6 loopback address (::1). 侦听套接字绑定到IPv6环回地址(:: 1)。 I recall some issues with Java not supporting dual-stack IPv4/IPv6 systems correctly; 我回想起一些Java问题,它们不正确地支持双栈IPv4 / IPv6系统。 this is probably such a case. 这可能是这种情况。 It is connecting to 127.0.0.1 only (IPv4). 它仅连接到127.0.0.1(IPv4)。

Everything else you have tried (curl, telnet...) will try the IPv6 address first, and then fall back on the IPv4 address if that fails. 您尝试过的所有其他操作(curl,telnet ...)都将首先尝试IPv6地址,然后在失败的情况下使用IPv4地址。 That's why they work, while the Java application does not. 这就是为什么它们起作用,而Java应用程序却不能起作用的原因。

Try forcing stunnel to bind to 127.0.0.1. 尝试强制将tunnel绑定到127.0.0.1。 You might also try having Java connect to http://[::1]:8045/malibu/GetProviders , though I can't recall if it supports IPv6 addresses in HTTP URLs. 您也可以尝试将Java连接到http://[::1]:8045/malibu/GetProviders ,尽管我不记得它是否支持HTTP URL中的IPv6地址。

I have Apache on Windows and also connection refused from Java. 我在Windows上具有Apache,并且Java拒绝了连接。 However debugging the connection and the Apache log shows, that it is actually not a connection problem . 但是调试连接和Apache日志显示,这实际上不是连接问题 Apache returns error 301, permanently moved. Apache返回错误301,已永久移动。 Then it provides a redirection url to non-existing port 8080. So something's wrong with the server configuration, probably ServerName directive uses wrong port. 然后,它提供了指向不存在的端口8080的重定向URL。因此服务器配置有问题,可能是ServerName指令使用了错误的端口。 Adding a trailing slash to the requested url fixes the problem. 在请求的网址后面添加斜杠可解决此问题。 The most useful debugging output in my case was given by wget. 在我的情况下,最有用的调试输出是由wget提供的。

It's possible that the accepted answer does not explain the phenomenon. 接受的答案可能无法解释这种现象。 The reporter himself admitted in a comment that finally he used a url with slash at the end. 记者本人在评论中承认,他最终在结尾处使用了带斜杠的网址。

暂无
暂无

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

相关问题 RMI连接被拒绝与本地主机 - Rmi connection refused with localhost RMI连接在本地主机上被拒绝 - RMI connection refused on localhost 本地主机连接在springboot中被拒绝 - localhost connection refused in springboot 连接被拒绝:localhost/0:0:0:0:0:0:0:1:9000 与 gRPC - Connection refused: localhost/0:0:0:0:0:0:0:1:9000 with gRPC Glassfish Netbeans附加到localhost“连接被拒绝” - Glassfish Netbeans Attaching to localhost “Connection refused” Spring 在 localhost 上启动应用程序:连接被拒绝 - Spring Boot app on localhost: connection refused HttpHostConnectException:连接到 localhost:2375 [localhost/127.0.0.1] 失败:连接被拒绝 - HttpHostConnectException: Connect to localhost:2375 [localhost/127.0.0.1] failed: Connection refused TestRestTemplate HttpHostConnectException:连接到本地主机:8082 [localhost/127.0.0.1] 失败:连接被拒绝 - TestRestTemplate HttpHostConnectException: Connect to localhost:8082 [localhost/127.0.0.1] failed: Connection refused 无法执行 HTTP 请求:连接到 localhost:8000 [localhost/127.0.0.1] 失败:连接被拒绝(连接被拒绝) - Unable to execute HTTP request: Connect to localhost:8000 [localhost/127.0.0.1] failed: Connection refused (Connection refused) Eclipse 4.23.0 连接拒绝主机:localhost; 嵌套异常是:java.net.ConnectException:连接被拒绝:连接 - Eclipse 4.23.0 Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM