Java 连接在远程 IP 上被拒绝,但在本地 IP 上被接受

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18619195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:35:07  来源:igfitidea点击:

Connection refused on remote IP, but accepted on local IP

javaandroid

提问by adnan_e

As the title says, I have my server running on a local machine, I tested and debugged it and it worked perfectly (server is written in java as well). But when I tried to test it with my remote IP (instead of 192.168.0.113 I used 146.255.x.x), and the server didn't receive anything, while the client has thrown this:

正如标题所说,我的服务器在本地机器上运行,我对其进行了测试和调试,并且运行良好(服务器也是用 java 编写的)。但是当我尝试用我的远程 IP(而不是 192.168.0.113 我使用 146.255.xx)测试它时,服务器没有收到任何东西,而客户端抛出了这个:

09-04 18:23:27.595: W/System.err(24241): java.net.ConnectException: failed to connect to /146.255.x.x (port 4040): connect failed: ECONNREFUSED (Connection refused)
09-04 18:23:27.595: W/System.err(24241):    at libcore.io.IoBridge.connect(IoBridge.java:114)
09-04 18:23:27.595: W/System.err(24241):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-04 18:23:27.595: W/System.err(24241):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-04 18:23:27.595: W/System.err(24241):    at java.net.Socket.startupSocket(Socket.java:566)
09-04 18:23:27.595: W/System.err(24241):    at java.net.Socket.tryAllAddresses(Socket.java:127)
09-04 18:23:27.595: W/System.err(24241):    at java.net.Socket.<init>(Socket.java:177)
09-04 18:23:27.595: W/System.err(24241):    at java.net.Socket.<init>(Socket.java:149)
09-04 18:23:27.595: W/System.err(24241):    at com.statenislandchat.Main.run(Main.java:146)
09-04 18:23:27.595: W/System.err(24241): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
09-04 18:23:27.595: W/System.err(24241):    at libcore.io.Posix.connect(Native Method)
09-04 18:23:27.600: W/System.err(24241):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
09-04 18:23:27.600: W/System.err(24241):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-04 18:23:27.600: W/System.err(24241):    at libcore.io.IoBridge.connect(IoBridge.java:112)
09-04 18:23:27.600: W/System.err(24241):    ... 7 more

I disabled the firewall on the server PC long ago, and I DID forward the ports on my router. I tried with some portscanners and they all detect a service on my port, but my client fails to reach it again and again.

我很久以前禁用了服务器 PC 上的防火墙,并且我确实转发了路由器上的端口。我尝试了一些端口扫描器,它们都检测到我端口上的服务,但是我的客户端一次又一次地无法访问它。



SOLVED!I'm not much into networking but it seems that its impossible to connect like this if both the server and client are on the same router. I tried connecting from a distant client and it worked.

解决了!我不太喜欢网络,但如果服务器和客户端都在同一个路由器上,似乎不可能像这样连接。我尝试从远程客户端连接,它奏效了。

采纳答案by Tassos Bassoukos

ECONNREFUSED means that the connection was attempted and the remote host answered back that nobody's listening on that port. That could mean several things:

ECONNREFUSED 表示已尝试连接并且远程主机回答说没有人在该端口上侦听。这可能意味着几件事:

  1. Is that really your IP? What does ifconfig/ipconfig say (hint: not necessarily what whatismyip.com will say)?
  2. Does the Java server listen on all interfaces? the same port could be on localhost open and closed on other interfaces. Check your bind addess on the server.
  3. Try a telnet + tcpdump, does it show that packets go to the correct address?
  1. 那真的是你的IP吗?ifconfig/ipconfig 说什么(提示:不一定 whatismyip.com 会说什么)?
  2. Java 服务器是否侦听所有接口?相同的端口可以在本地主机上打开并在其他接口上关闭。检查您在服务器上的绑定地址。
  3. 尝试 telnet + tcpdump,它是否显示数据包到达正确的地址?

And these are just the start.

而这些仅仅是开始。

回答by Peter Lund

I also had problems getting my Android Studio java TCP client to connect to my local server on my development machine. I got it working by fixing 2 things:

我在让我的 Android Studio java TCP 客户端连接到我的开发机器上的本地服务器时也遇到了问题。我通过修复两件事让它工作:

  1. Do not try to open a socket towards "127.0.0.1" or towards "localhost". Instead figure out your local address. In my case it worked with "192.168.0.100" which is my dev computers local address.

  2. Do not try to create the "new Socket(address,portnumber)" in the main thread of the android java app. I got it working by creating the socket in another thread. This was a temporary thread that only created the Socket and stored the reference before dying.

  1. 不要尝试向“127.0.0.1”或“localhost”打开套接字。而是找出您的本地地址。就我而言,它与“192.168.0.100”一起使用,这是我的开发计算机本地地址。

  2. 不要尝试在 android java 应用程序的主线程中创建“new Socket(address,portnumber)”。我通过在另一个线程中创建套接字来让它工作。这是一个临时线程,它只创建 Socket 并在死亡之前存储引用。

Exception for problem 2

问题 2 的例外

When creating socket in main thread:

在主线程中创建套接字时:

11-29 11:00:35.718 27063-27063/se.mycompany.tcpclient2 E/AndroidRuntime: 
FATAL EXCEPTION: main
Process: se.mycompany.tcpclient2, PID: 27063
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
...