简体   繁体   中英

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

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. I tried with some portscanners and they all detect a service on my port, but my client fails to reach it again and again.


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. That could mean several things:

  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?

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. I got it working by fixing 2 things:

  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.

Exception for problem 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)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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