B4J Question Find my IP address

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I am trying to get a component of my local IP address so as to know which server I am attached too.

Finding the local IP address is working correctly but Regex.split is not splitting the IP address into its components.

Can someone please give me an idea as to what is wrong with my code.

I get a zero length on components() and an out of bounds error on component(2) request.

Thanks
IP Address:
  'Get local IP
    socket.Initialize(8888,"")
    myLocalIP = socket.GetMyIP
    Log(DateTime.Time(DateTime.Now) & " MyIP is " & myLocalIP)
    Log("My IP = "&myLocalIP)
    Dim components() As String
    components = Regex.Split(".",myLocalIP)
    Log("Components len = "&components.Length)
    If components(2) = "20" Then
        serverIP = "192.168.20.128"
    Else
        serverIP = "192.168.1.128"
    End If
    socket.Close
 

atiaust

Active Member
Licensed User
Longtime User
Thank you for your reply.

After further searches I found that with Regex.Split you need to include a "\" before the "." in the split. ie Regex.Split("\.",myLocalIP).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
socket.Initialize(8888,""

1) you don't have to initialize the socket in order to use GetMyIP

2) you could use StartsWith instead of regular expressions
B4X:
If myLocalIP.StartsWith("192.168.20") Then
        serverIP = "192.168.20.128"
    Else
        serverIP = "192.168.1.128"
    End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…