Thanks peoples for help,
The most easiest way I will use is calling an API or webservice or check whether opening google.com return expected output using OkHttpUtils2
to
@aeric and
@teddybear yes I already done this, but the problem is that make an http request while there is network, it will work successfully, if there is no network, the log show the error that seem I cannot avoid. Because I have to use this for a library I don't like it, instead I wanted to log only my error.
I've used this code, but anyway this have nothing to do with the problem that Socket.GetMyIP return wrong IP address. Before now I never had the necessity to make an http request to know if the device is connected, I do this only to get the public ip address, so the true problem is a wrong network configuration inside Windows. After this will work I can continue to use Socket.GetMyIP.
B4X:
' 1. Test connettività
Dim j As HttpJob
j.Initialize("", Me)
' Endpoint leggero e affidabile
j.Download("https://www.google.com/generate_204")
j.GetRequest.Timeout = 3000 ' 3 secondi per evitare attese lunghe
Wait For (j) JobDone(j As HttpJob)
Dim online As Boolean = (j.Success And j.Response.StatusCode = 204)
j.Release
If Not(online) Then
Log ("Your device is not connected. Please connect and try again.")
Return "127.0.0.1"
End If
As for
@Magma suggertion now I completely disabled IPv6 on my Fritz and on my Windows, now Socket.GetMyIP return my real IP when online, but return wrong IP (not the software loopback 127.0.0.1) when not online. Instead it see VirtualBox network IP. Disabling the VirtualBox network it see Hyper-V IP address.
@Magma now I will read better last two your messages and your advices, I'm a bit busy here.
EDIT: I read a bit better, but I had no more time to test all things. But this set the static IP address instead to use DHCP, I don't want to do this.
The problem here is that (after I having this problem some days ago) I uninstalled from Control Panel the additional networks for VirtualBox and Hyper-V
they are duplicated for some reasons, like the same but with #2 or #3 index, but I only doing this after the problem occoured, not before.
Now suppose that VirtualBox do not exists, I have it installed but last time I've used it one year ago, so now I will completely uninstall and it is another problem removed.
Hyper-V cannot be uninstalled, only disabed, this way the default switch will be removed, but I don't want to disable it.
But the big problem is that this only happen when I use Socket.GetMyIP on B4J, using ipconfig and from Windows settings all seem ok but something in background is wrong, may network metrics and interfaces priorities.
Now I will try to completely uninstall VirtualBox, but for sure after that it see Hyper-V.