When I connect to a VPN, I am unable to connect to devices on the local network via socket.
https://i.sstatic.net/2fAabSkM.png
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build();
connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
try {
Socket socket = new Socket();
network.bindSocket(socket);
socket.connect(new InetSocketAddress("192.168.42.1",6666));
Log.i(TAG, "success");
} catch (IOException e) {
Log.i(TAG, e.getMessage());
}
}
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
}
});
I have already declared the following network permissions.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
The above is my code, and the system reported an ‘Binding socket to network 124 failed: EPERM (Operation not permitted)‘ error.
I tried removing the capability NetworkCapabilities.NET_CAPABILITY_NOT_VPN, but the socket still cannot connect.
removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
If you have a VPN connection, all the data traffic is rerouted via the VPN network and you won’t be able to see the devices in your local network. try switching off VPN to discover local devices.