Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot connect to WebSocket Server on local network (via wifi + no internet) if mobile data is on (with internet). #814

Closed
robinchew opened this issue Nov 19, 2018 · 9 comments

Comments

@robinchew
Copy link

robinchew commented Nov 19, 2018

Describe the bug

On my Android phone I have both wifi and mobile data on. But wifi has NO internet and mobile data has internet. My Android app uses Java-WebSocket to connect to the websocket server via IP (eg. 192.168.1.3) but times out. But if mobile data is turned off, then it will work.

To Reproduce
Steps to reproduce the behavior:

  1. Have a WebSocket server (eg. with IP 192.168.1.3) on your local area network.
  2. Your LAN has no internet
  3. On your Android phone, switch on Wifi to connect to the LAN with no internet.
  4. On your Android phone, switch on Mobile data so you do have internet.
  5. Run Android app that runs code that connects to the WebSocket Server, eg:
MyWebSocketClient(URI("ws://192.168.1.3:8888/websocket-handler/"))

Example application to reproduce the issue
Do I really need to?

Expected behavior
It should just work. If not, then how do you explicitly tell the WebSocketClient to connect to the Wifi network instead of the Mobile network?

Workaround
Switch off mobile data.

Debug log

W/System.err: an error occurred: java.net.ConnectException: failed to connect to /192.168.1.3 (port 8888) from /:: (port 46537): connect failed: ETIMEDOUT (Connection timed out)
I/System.out: closed with exit code -1 additional info: failed to connect to /192.168.1.3 (port 8888) from /:: (port 46537): connect failed: ETIMEDOUT (Connection timed out)

Environment(please complete the following information):

  • Version used: 1.3.9
  • Java version: 1.7 or 1.8
  • Operating System and version: ArchLinux
  • Endpoint Name and version:
  • Link to your project:

Additional context
None

@marci4 marci4 added the Android label Dec 6, 2018
@marci4
Copy link
Collaborator

marci4 commented Dec 6, 2018

Hello @robinchew,

Is there any android setting with which you can define which connection is used?

This is a java lib and therefore we cannot use internal android apis. But I am pretty sure someone already had such an issue before you.

In my eyes it has to be defined by the application and not by the lib (and we simple cannot).

Best regards,
Marcel

@marci4
Copy link
Collaborator

marci4 commented Jan 9, 2019

Any update ?

@robinchew
Copy link
Author

I'm still unsure how to solve it on the Android side.

@marci4
Copy link
Collaborator

marci4 commented Jan 9, 2019

If you can't solve it in android how can I even solve this in java?

@robinchew
Copy link
Author

If this isn't a Java-WebSocket issue, but an Android issue, then I'm open to solutions. Unfortunately I haven't found one yet, hopefully someone hitting this same issue is able to provide some insight.

@robinchew
Copy link
Author

Initially, I thought perhaps it was possible to pass eg. networkInterface as an argument to Java-WebSocket somehow, and you are able to connect to eg. 192.168.1.3 on 1 network and the same IP on another network.

@letmewearshoes
Copy link

I have the same problem, and don't know how to fix it. The connection is okay with wi-fi, but does not work at all on cellular. Please help

@olivierayache
Copy link
Contributor

I fix the same problem by using the following solution:
On Android you can force a process to use a specific network by using https://developer.android.com/reference/android/net/ConnectivityManager#bindProcessToNetwork(android.net.Network). It is available from version 23.
Moreover Java-WebSocket allow you to set a custom SocketFactory (method setSocketFactory() in WebSocketClient class). You can use the SocketFactory from your Network (https://developer.android.com/reference/android/net/Network.html#getSocketFactory()).

@marci4
Copy link
Collaborator

marci4 commented Mar 1, 2021

Closing this issue as a solution is provided

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants