Perplexity:
Here's a more complete B4J code for obtaining a reliable IP address, which takes into account issues with multiple virtual interfaces and the possibility of a disconnected network. The idea is:
Use JavaObject to iterate through all network interfaces
Filter virtual IPs, loopback, link-local (APIPA), and IPv6
If no valid IP is found, force a return of 127.0.0.1
Add a network connectivity check using a ping attempt to decide whether to use network IPs or force a fallback to loopback.
Sub GetReliableIPv4 As String
Dim NI As JavaObject
Dim Runtime As JavaObject
Runtime.InitializeStatic("java.lang.Runtime")
Dim enNI As JavaObject
NI.InitializeStatic("java.net.NetworkInterface")
enNI = NI.RunMethod("getNetworkInterfaces", Null)
If enNI.IsInitialized = False Then Return "127.0.0.1"
Dim foundIP As String = ""
' Funzione interna per ping test (ret true se ping a 8.8.8.8 va a buon fine)
Sub CheckInternetConnectivity As Boolean
Try
Dim Process As JavaObject = Runtime.RunMethod("exec", Array("ping -n 1 8.8.8.8"))
Dim InputStream As Object = Process.RunMethod("getInputStream", Null)
Dim Scanner As JavaObject
Scanner.InitializeNewInstance("java.util.Scanner", Array(InputStream))
Dim output As String = ""
Do While Scanner.RunMethod("hasNextLine", Null)
output = output & Scanner.RunMethod("nextLine", Null) & CRLF
Loop
Return output.ToLowerCase.Contains("reply from")
Catch
Return False
End Try
End Sub
Dim hasConnectivity As Boolean = CheckInternetConnectivity
Do While enNI.RunMethod("hasMoreElements", Null)
Dim netInterface As JavaObject = enNI.RunMethod("nextElement", Null)
' saltare interfacce non attive o di loopback
If netInterface.RunMethod("isUp", Null) = False Then Continue
If netInterface.RunMethod("isLoopback", Null) = True Then Continue
Dim enAddr As JavaObject = netInterface.RunMethod("getInetAddresses", Null)
Do While enAddr.RunMethod("hasMoreElements", Null)
Dim ia As JavaObject = enAddr.RunMethod("nextElement", Null)
Dim ip As String = ia.RunMethod("getHostAddress", Null)
If ip.Contains(":") Then Continue ' IPv6
' ignorare loopback o localhost
If ip.StartsWith("127.") Then Continue
' ignorare APIPA address VirtualBox
If ip.StartsWith("169.254.") Then Continue
' ignorare subnet Hyper-V 172.16.0.0 - 172.31.255.255
If ip.StartsWith("172.") Then
Dim parts() As String = Regex.Split("\.", ip)
Dim secondOctet As Int = parts(1)
If secondOctet >= 16 And secondOctet <= 31 Then Continue
End If
' se c'è connettività internet, accettare il primo IP valido
If hasConnectivity Then
Return ip
Else
' se non c'è connettività internet, salvare l'IP se non è virtuale noto
foundIP = ip
End If
Loop
Loop
' Se no connettività e non abbiamo trovato IP valido diverso, fallback a 127.0.0.1
If foundIP <> "" Then Return foundIP
Return "127.0.0.1"
End Sub
Explanations
CheckInternetConnectivity pings 8.8.8.8 to determine if the machine has external access.
If the network is reachable, it returns the first valid IP that is not virtual, loopback, APIPA, or IPv6.
If the network is unreachable, it searches for a valid non-virtual IP, but otherwise returns 127.0.0.1.
This approach balances the issue of multiple virtual interfaces and the lack of a physical network.
This method can be integrated into your project to replace ServerSocket.GetMyIP and obtain a more reliable result under all network conditions.