get host name

fanfalveto

Active Member
Licensed User
Longtime User
please anyone can help me with this
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      socket1.Initialize(8000, "Server")
      If socket1.IsInitialized Then
         socket1.Listen
         Log("Android is listening for a incomming connection")
      End If
      Params(0) = "192.168.1."
   End If
BuscaNombreHost(Params(0))
End Sub

Sub BuscaNombreHost(dirIP As String)
      Dim infostring As String
      lstPings.Clear   'a Listview
      For i=1 To 255
         Try 
            infostring = GetHostName("192.168.1."&i)             
            lstPings.AddSingleLine(infostring & " --> Conectado") 
            Log(infostring & " --> Conectado")
         Catch
            lstPings.AddSingleLine(infostring & " --> No conectado")
            Log(infostring & " --> NO Conectado")
         End Try
      Next
End Sub

Sub GetHostName(IP As Object) 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("getHostName")   
End Sub
I want to get all the host names of the network 192.168.1.--
Thank you
 

fanfalveto

Active Member
Licensed User
Longtime User
I've seen a code (can not remember the post) on why I did this
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Socket1 As Socket                                
    Dim PingTimer,PingTimer2,PingTimer3,PingTimer4,PingTimer5,PingTimer6,PingTimer7,PingTimer8 As Timer   
    Dim IPAddress As String                                
    Dim IPPort As String         : IPPort = "135"        
    Dim MachineNode As Int        : MachineNode = 2        
    Dim NetworkNode As Int        : NetworkNode = 1        
    Dim lblInfo As Label                           
   Dim lstPings As ListView
   Dim inicio, final As Int
   Dim btnEscanea As Button
   Dim txtIp4 As EditText
   Dim txtIp8 As EditText
   Dim txtIp3 As EditText
   Dim timerActual As Int
   Dim infostring As String
   Dim ipp As Object
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Activity.LoadLayout("menu")                        
        PingTimer.Initialize("PingTimer", 50)            
      PingTimer2.Initialize("PingTimer2", 50)
      PingTimer3.Initialize("PingTimer3", 50)
      PingTimer4.Initialize("PingTimer4", 50)
      PingTimer5.Initialize("PingTimer5", 50)
      PingTimer6.Initialize("PingTimer6", 50)
      PingTimer7.Initialize("PingTimer7", 50)
      PingTimer8.Initialize("PingTimer8", 50)
      Activity.AddMenuItem("Salir","Salida")
    End If
    'Formato del listview
   'Tamaño de letra del listview
   Dim Lb As Label
   Lb = lstPings.SingleLineLayout.Label
   Lb.Textsize = 12
   'Alto de filas del listview
   lstPings.SingleLineLayout.ItemHeight = 50
End Sub

'Salir de la aplicación
Sub Salida_Click()
   Activity.Finish
   ExitApplication
End Sub

Sub Socket1_Connected (Successful As Boolean)            
    If Successful Then   
       'Si el elemento no está en la lista
      
       

      If CompruebaLista(IPAddress) = False Then 
      infostring = GetHostName(IPAddress) 
      lstPings.AddSingleLine(IPAddress&"  "&infostring)
'       
       Socket1.Close
      End If
    Else
        If timerActual = 1 Then PingTimer.Enabled = True                        
       If timerActual = 2 Then PingTimer2.Enabled = True
      If timerActual = 3 Then PingTimer3.Enabled = True
      If timerActual = 4 Then PingTimer4.Enabled = True
      If timerActual = 5 Then PingTimer5.Enabled = True
      If timerActual = 6 Then PingTimer6.Enabled = True
      If timerActual = 7 Then PingTimer7.Enabled = True
   End If
End Sub

'Escanear puerto 80 HTTP
Sub PingTimer_Tick
    PingTimer.Enabled = False
    MachineNode = MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 80"
        Socket1.Close
        Socket1.Initialize("Socket1")
        Socket1.Connect(IPAddress, 80, 1000)
        PingTimer.Enabled = True
   Else
      PingTimer.Enabled = False
      MachineNode = 0
      timerActual = 2
      PingTimer2.Enabled = True
    End If
End Sub

'Escanear puerto 135 NETBIOS
Sub PingTimer2_Tick
    PingTimer2.Enabled = False
    MachineNode =  MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 135"
        Socket1.Close
        Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, 135, 1000)
        PingTimer2.Enabled = True
   Else
      PingTimer2.Enabled = False
      MachineNode = 0
      timerActual = 3
      PingTimer3.Enabled = True
    End If
End Sub

'Escanear puerto 139 SHARED
Sub PingTimer3_Tick
    PingTimer3.Enabled = False
    MachineNode =  MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 139"
        Socket1.Close
        Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, 139, 1000)
        PingTimer3.Enabled = True
   Else
      PingTimer3.Enabled = False
      MachineNode = 0
      timerActual = 4
      PingTimer4.Enabled = True
    End If
End Sub

'Escanear puerto 21 FTP
Sub PingTimer4_Tick
    PingTimer4.Enabled = False
    MachineNode =  MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 21"
        Socket1.Close
        Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, 139, 1000)
        PingTimer4.Enabled = True
   Else
      PingTimer4.Enabled = False
      MachineNode = 0
      timerActual = 5
      PingTimer5.Enabled = True
    End If
End Sub

'Escanear puerto 22 SSH
Sub PingTimer5_Tick
    PingTimer5.Enabled = False
    MachineNode =  MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 22"
        Socket1.Close
        Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, 139, 1000)
        PingTimer5.Enabled = True
   Else
      PingTimer5.Enabled = False
      MachineNode = 0
      timerActual = 6
      PingTimer6.Enabled = True
    End If
End Sub

'Escanear puerto 23 TELNET
Sub PingTimer6_Tick
    PingTimer6.Enabled = False
    MachineNode =  MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 23"
        Socket1.Close
        Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, 139, 1000)
        PingTimer6.Enabled = True
   Else
      PingTimer6.Enabled = False
      MachineNode = 0
      timerActual = 7
      PingTimer7.Enabled = True
    End If
End Sub

'Escanear puerto 3389 RDP
Sub PingTimer7_Tick
    PingTimer7.Enabled = False
    MachineNode =  MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 3389"
        Socket1.Close
        Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, 139, 1000)
        PingTimer7.Enabled = True
   Else
      PingTimer7.Enabled = False
      MachineNode = 0
      timerActual = 8
      PingTimer8.Enabled = True
    End If
End Sub

'Escanear puerto 445 SMB over TCP
Sub PingTimer8_Tick
    PingTimer8.Enabled = False
    MachineNode =  MachineNode + 1
    If MachineNode <= final Then                            
        IPAddress = "192.168." & NetworkNode & "." & MachineNode
        lblInfo.Text = "Trying " & IPAddress & " on port 445"
        Socket1.Close
        Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, 445, 1000) 
        PingTimer8.Enabled = True
   Else
      PingTimer8.Enabled = False
      lblInfo.Text = lstPings.Size & " dispositivos encontrados"
      ToastMessageShow("Escaneo completado",False)
    End If
End Sub

'Comprobar si el elemento ya está en la lista
Sub CompruebaLista(texto As String) As Boolean
   For i=0 To lstPings.Size - 1
      If lstPings.GetItem(i) = texto Then
         Return True
      End If
   Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'Empieza a escanear
Sub btnEscanea_Click
   inicio = txtIp4.Text
   final = txtIp8.Text
   NetworkNode = txtIp3.text
   MachineNode = inicio-1
   PingTimer.Enabled = True    
   timerActual = 1
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
and i have this error
B4X:
main_gethostname (java line: 347)


android.os.NetworkOnMainThreadException

   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
   at java.net.InetAddress.getHostByAddrImpl(InetAddress.java:438)
   at java.net.InetAddress.getCanonicalHostName(InetAddress.java:320)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:205)
   at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod(Reflection.java:791)
   at aaa.wifi.main._gethostname(main.java:347)
   at aaa.wifi.main._socket1_connected(main.java:703)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
   at anywheresoftware.b4a.BA$3.run(BA.java:303)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5191)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
   at dalvik.system.NativeStart.main(Native Method)
android.os.NetworkOnMainThreadException
i see that in InetAddress | Android Developers ,please can you help me,I'm not very good with java and reflection.
it´s work fine scan range of ip on these ports,but i can´t get the host names.
Thank you
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You should post your project, export it as ZIP, you have a layout there, without it, it will be difficult to test your code.

Another thing, this line:
B4X:
Activity.LoadLayout("menu")
Should NOT be in If FirstTime Then ...
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Replace this line in your manifest:

From:
B4X:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
To:
B4X:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="4"/>

Also, like I mentioned above remove the line:
B4X:
Activity.LoadLayout("menu")
out of If FirstTime Then..., or your code will fail when you rotate the device.
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
thank you,don´t get the hostname but at least the program does not close.
thank you very much :sign0188:
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
whit this
B4X:
Sub GetHostName(ipp As String) As String
    Dim r As Reflector
    r.Target = r.RunPublicmethod("getCanonicalHostName", Array As Object(ipp), _
        Array As String("java.lang.String"))
    If r.Target = Null Then Return "N/A"
    Return r.RunMethod("getCanonicalHostName")
End Sub
i get this errorn when compile
B4X:
Parsing code.                           0.00
Compiling code.                         0.06
Compiling layouts code.                 0.02
Generating R file.                      0.17
Compiling generated Java code.          Error
B4A line: 143
r.Target = r.RunPublicmethod(\
javac 1.6.0_26
src\aaaaaaa\wifi\main.java:392: inconvertible types
found   : java.lang.String[]
required: java.lang.Class[]
_r.Target = _r.RunPublicmethod("getCanonicalHostName",new Object[]{(Object)(_ipp)},(java.lang.Class[])(new String[]{"java.lang.String"}));                                                                                                   ^
1 error
please any idea,i want get hostname ,I have tried many ways but nothing.
Thank you
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
1) You need the latest Reflection library 2.4 which fixes Run PublicMethod.

BUT

2a) You have not set r.Target to any object instance.

2b) I assume you want to invoke InetAddress.getCanonicalHostName() on an InetAddress instance but there is so such method as InetAddress.getCanonicalHostName(String IpAddress).

2c) You will need to invoke one of the InetAddress static methods by RunStaticMethod to get an InetAddress instance in r.Target.

This works but may well only return the provided IP address as the Host Name

B4X:
Sub ShowHostName
   Dim r As Reflector
   Dim ip() As Byte = Array As Byte(192, 168, 0, 1)
   r.Target = r.RunStaticMethod("java.net.InetAddress", "getByAddress", Array As Object( ip ), Array As String("[B"))
   r.Target = r.RunMethod("getCanonicalHostName")   
   Msgbox(r.Target, "Host Name")

End Sub
 
Upvote 0
Top