Android Question How to get Local IP Address from Computer Name ?

Rajesh Gupta

Member
Licensed User
Dear All

Suppose my computer name is "Utkarsh", and i need to get my Local Ip address using my computer name i.e "Utkarsh".

Is there any way ?

Thanks
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
Open cmd and type ipconfig
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
No idea if this is any help. Using inline java: Link
 
Upvote 0

Rajesh Gupta

Member
Licensed User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
   
    Dim x As String
    x = GetHostName("dotnet")
    Msgbox(x, "IP Address of host")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub GetHostName(IP As String) As String
    Dim r As Reflector
    r.Target = r.RunStaticMethod("java.net.InetAddress", "getByName", Array As Object(IP), _
        Array As String("java.lang.String"))
    If r.Target = Null Then Return "N/A"
    Return r.RunMethod("getCanonicalHostName")
End Sub

This code is giving exception - java.lang.UnknownHostException
 
Upvote 0

Rajesh Gupta

Member
Licensed User
See my answer in post #5.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    serversocket.Initialize("serversocket")
    serversocket.Connect("Dotnet", 1111 , 1000)
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub GetHostName(Ip As String) As String
    Dim r As Reflector
    r.Target = r.RunStaticMethod("java.net.InetAddress", "getByName", Array As Object(Ip), _
        Array As String("java.lang.String"))
    If r.Target = Null Then Return "N/A"
    Return r.RunMethod("getCanonicalHostName")
End Sub

Sub serversocket_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
   
    Dim x As String
    x = GetHostName("Dotnet")
    Msgbox(x, "IP Address of host")
    serversocket.Close
End Sub


Here the Error comes - android.system.gaiexception android_getaddrinfo failed eai_nodata (No address associated with hostname)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Rajesh Gupta

Member
Licensed User
The GetHostName code is not required and will not help. The problem is related to the network configuration. The DNS server that the device uses is not able to resolve the host name.

BTW, there are many mistakes in this code. Network example: [B4X] Network + AsyncStreams + B4XSerializator

You can use UDP broadcasting to find the server: https://www.b4x.com/android/forum/threads/mqtt-chat-with-auto-discovery.75713/#content

So i need to change the network configuration and DNS on my Sytem?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Have you got Bonjour running on the computer that you want to connect to?
If not, could you install Bonjour on that computer?

I've recently wrapped the jmDNS library and have been using it with good results in a project i'm working on.
It takes a 'whatever.local' address and tries to resolve it to an IP address, you then use the IP address to connect to your computer.
(You can't connect to whatever.local, you must resolve to an IP address and connect using IP address).

If you want to try my jmDNS then let me know and i'll upload it.
 
Upvote 0

Rajesh Gupta

Member
Licensed User
Have you got Bonjour running on the computer that you want to connect to?
If not, could you install Bonjour on that computer?

I've recently wrapped the jmDNS library and have been using it with good results in a project i'm working on.
It takes a 'whatever.local' address and tries to resolve it to an IP address, you then use the IP address to connect to your computer.
(You can't connect to whatever.local, you must resolve to an IP address and connect using IP address).

If you want to try my jmDNS then let me know and i'll upload it.

Yaa sure..that would be great
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
OK here's my wrapper library for jmDNS.
I've attached a sample b4a project which contains a class named JmDNSManager, this class is what you're interested in.

You might also want to have a read of https://home.heeere.com/tech-androidjmdns.html, which states:
To improve battery life, processing of multicast packets is disabled by default on Android. We can and must reenable this for the service discovery to work. This is done programmatically by acquiring a lock in our activity.

I don't have time to document anything so you'll have to modify lines 16 and 17 of Main activity:

B4X:
Private Const TORRENT_SERVER_SERVICE_NAME As String="torrentserver"
Private Const TORRENT_SERVER_SERVICE_TYPE As String="_websocket._tcp.local."

Then compile and see if your local domain is discovered and resolved.

Logcat on my pc shows success:
JmDNSManager1_Initialized: true
mServiceListener_ServiceAdded: torrentserver
mServiceListener_ServiceResolved: torrentserver
JmDNSManager1_ServiceResolved
192.168.100.100
torrentserver
8080

Library files and example project attached.
 

Attachments

  • jmDNS_library_files.zip
    242.8 KB · Views: 381
  • jmDNS_example_project.zip
    8.9 KB · Views: 358
Upvote 0

Rajesh Gupta

Member
Licensed User
OK here's my wrapper library for jmDNS.
I've attached a sample b4a project which contains a class named JmDNSManager, this class is what you're interested in.

You might also want to have a read of https://home.heeere.com/tech-androidjmdns.html, which states:


I don't have time to document anything so you'll have to modify lines 16 and 17 of Main activity:

B4X:
Private Const TORRENT_SERVER_SERVICE_NAME As String="torrentserver"
Private Const TORRENT_SERVER_SERVICE_TYPE As String="_websocket._tcp.local."

Then compile and see if your local domain is discovered and resolved.

Logcat on my pc shows success:


Library files and example project attached.

Thanks for the help, but i am not quite getting it..
Simply my question is I want to enter my computer's HostName i.e "Dotnet" and the B4a program should resolveits Local IP Address i.e "192.168.0.44"
 
Upvote 0
Top