[英]Connection refused on remote IP, but accepted on local IP
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). 如标题所示,我的服务器在本地计算机上运行,我对其进行了测试和调试,并且运行良好(服务器也是用Java编写的)。 But when I tried to test it with my remote IP (instead of 192.168.0.113 I used 146.255.xx), and the server didn't receive anything, while the client has thrown this: 但是,当我尝试使用远程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$2$1$1.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. 很久以前,我在服务器PC上禁用了防火墙,并且DID转发了路由器上的端口。 I tried with some portscanners and they all detect a service on my port, but my client fails to reach it again and again. 我尝试使用一些portcanners,它们都在我的端口上检测到服务,但是我的客户端一次又一次无法访问它。
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. 我尝试从远处的客户端进行连接,但该方法有效。
ECONNREFUSED means that the connection was attempted and the remote host answered back that nobody's listening on that port. ECONNREFUSED表示已尝试连接,并且远程主机答复说没有人在该端口上监听。 That could mean several things: 那可能意味着几件事:
And these are just the start. 这些仅仅是开始。
I also had problems getting my Android Studio java TCP client to connect to my local server on my development machine. 我也无法让我的Android Studio Java TCP客户端连接到开发机器上的本地服务器。 I got it working by fixing 2 things: 我通过修复2件事使其工作:
Do not try to open a socket towards "127.0.0.1" or towards "localhost". 不要尝试打开朝向“ 127.0.0.1”或“ localhost”的套接字。 Instead figure out your local address. 而是找出您的本地地址。 In my case it worked with "192.168.0.100" which is my dev computers local address. 就我而言,它与“ 192.168.0.100”一起使用,这是我的开发计算机的本地地址。
Do not try to create the "new Socket(address,portnumber)" in the main thread of the android java app. 不要尝试在android java应用程序的主线程中创建“ new Socket(address,portnumber)”。 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. 这是一个临时线程,仅在死前创建Socket并存储引用。
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) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.