简体   繁体   English

远程IP拒绝连接,但本地IP接受连接

[英]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: 那可能意味着几件事:

  1. Is that really your IP? 那真的是您的IP吗? What does ifconfig/ipconfig say (hint: not necessarily what whatismyip.com will say)? ifconfig / ipconfig说什么(提示:不一定是whatismyip.com会说什么)?
  2. Does the Java server listen on all interfaces? Java服务器是否在所有接口上侦听? 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? 尝试使用telnet + tcpdump,它是否显示数据包到达正确的地址?

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件事使其工作:

  1. 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”一起使用,这是我的开发计算机的本地地址。

  2. 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并存储引用。

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)
...

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

相关问题 如何建立远程IP的套接字连接? (我知道如何针对本地IP执行此操作) - How to make socket connection for remote IP ? (I know how to do it for Local IP) 尝试与外部IP连接时连接被拒绝 - Connection refused when trying to connect with an external IP Java Socket连接被我的公共IP拒绝 - Java Socket connection refused with my Public IP 通过 IP 地址而不是 localhost 连接到 RabbitMQ 时连接被拒绝 - Connection refused when connecting to RabbitMQ via IP address instead of localhost java.rmi.ConnectException:连接被拒绝托管: <public IP> - java.rmi.ConnectException: Connection refused to host: <public IP> DefaultHttpClient 调用在具有公共 ip 的同一个 tomcat 中抛出连接被拒绝 - DefaultHttpClient call throws connection refused in the same tomcat with public ip Java中来自远程客户端的本地Net IP地址 - Local Net Ip Address from a Remote Client in java 在java中获取远程tcp连接的ip地址? - Get remote tcp connection's ip address in java? 来电 <hostname> / <ip> 至 <hostname> :9000连接异常失败:java.net.ConnectException:连接被拒绝 - Call From <hostname>/<ip> to <hostname>:9000 failed on connection exception: java.net.ConnectException: Connection refused 错误:与主机的TCP / IP连接失败。 java.net.ConnectException:连接被拒绝:连接 - Error:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM