2

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:

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! It seems that that my router had client isolation active, which means that devices couldn't connect to each other on the same network. I tried connecting from a distant client and it worked.

adnan_e
  • 1,764
  • 2
  • 16
  • 25
  • `Connection refused` seems like an active rejection (as opposed to a timeout). – ashes999 Sep 04 '13 at 16:31
  • its thrown instantly, and my socket timeout is set setSoTimeout(0); (no timeout) for this test. – adnan_e Sep 04 '13 at 16:35
  • is your server accessible from outside of your local network? – Tobiel Sep 04 '13 at 16:43
  • I'm not sure I understood what you are saying. I'm having no problems connecting when I set the host to 192.168.0.113 (local IP), but when I put my remote IP or my NO-IP host, it wont connect, but will throw the error from the OP – adnan_e Sep 04 '13 at 17:07
  • When you said you used 146.255.x.x the "x.x" part are actually numbers right? You didn't literally type "x.x" right? – initramfs Sep 04 '13 at 17:27
  • actual numbers, lol. for example 146.255.142.152 – adnan_e Sep 04 '13 at 17:29

2 Answers2

4

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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • I tried both my NO-IP Host and direct IP, both resulted in the same exception. And since the port scanners detected the service on the same port I think the port is allright. I'm sure it's my server because my server binds to 3 ports (4040, 4041, 4042) and all 3 ports are detected by the port scanner. – adnan_e Sep 04 '13 at 17:06
  • Ok, the remote IP you mention is the IP address of your home DSL box/router, *NOT* the IP address of your computer. Add a port forwarding setting to your router to redirect port 4040 from the router to your computer's IP address, port 4040. – Tassos Bassoukos Sep 04 '13 at 18:27
  • Oh wait, you already mentioned that you have forwarded the port. Did you run a tcpdump/windump to see the actual network packets, and where they are routed to/from? It also might be that your router is not forwarding correctly from the inside interface, but only from the outside. – Tassos Bassoukos Sep 04 '13 at 18:37
  • 1
    FIXED IT! 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. – adnan_e Sep 04 '13 at 19:27
  • Depends on the make. My current home router is definitely able to do this, my previous one couldn't. – Tassos Bassoukos Sep 05 '13 at 12:33
  • Well my current home router is a Tenda which pretty much speaks for itself. It was the only one available at the time I was purchasing and the seller told me not to expect much from it, which I didn't. I will upgrade if I encounter more issues with it. – adnan_e Sep 05 '13 at 14:22
0

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)
...
Community
  • 1
  • 1