Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

android - java.net.ConnectException: failed to connect

I am trying to connect from my Android device to my server on the local network. The connection is made in two ways:

  1. HTTP, works fine all the time.

  2. Sockets. This works fine at work, and at home.

    Except when I connect to my corporate network through VPN at home.

We have eight dedicated Android devices (TC70s, if you must know) that connect to the local server through WiFi. We have been running this configuration for a few years now, works great. I haven't been concerned about this problem until yesterday, when one of the 8 devices at work had this connection issue. Now I am trying to figure out the problem before it gets worse.

When the Socket Connection fails, I get this exception: "failed to connect to /192.168.1.xxx (port xxxx): connect failed: ECONNREFUSED (Connection refused)"

Thoughts? Why can't I connect?

Code:

// void for return value, does not throw an exception.
new SecurityManager().checkConnect(Configuration.socketServer, Configuration.socketPort);

Socket socket = new Socket("192.168.1.xxx", "xxxx");

I tried turning off Windows Defender, Whitelisting my server app in Windows Defender, checking that nothing else is listening to the port, turn off Windows Firewall.

The port is added to the ACL on my computer, as well as the one running the server at work. The port is added to the firewall rules. And yes, I have this in the manifest: uses-permission android:name="android.permission.INTERNET"

question from:https://stackoverflow.com/questions/66055288/java-net-connectexception-failed-to-connect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When running VPN, server was attaching the Socket Listener to the VPN ip, not local IP. Code did not handle multiple IPs, just took first IP, the wrong one. Used netstat to see which IP the port was bound to. Need to rework code to handle multiple IPs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...