How does one get the Host Name

Status
Not open for further replies.

myriaddev

Active Member
Licensed User
Longtime User
Hi.
1.) I can get the Host address, but how do you
get the Host Name of the local network such as "jerryN" ?

2.) Android 2.2 asks user to login by sliding a slider. How
can one force the user to login using a password ?

Thanks, Jerry
 

myriaddev

Active Member
Licensed User
Longtime User
Control who logs into Device

Hi. Two questions:
1.) How does one control who gets into your Device ?
2.) How does one cause your program to run at startup of Device ?
Thanks, Jerry
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code (requires reflection):
B4X:
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

Going from IP address to host name is problematic. It depends on the DNS settings.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Thanks, this could become very handy although as you say, the result can vary according to DNS-settings.

Thanks also to Agraham for his amazing Reflection-library. What a life-saver it is . :sign0188:
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel, I can't seem to get this to work.
When I use:
B4X:
'Activity module
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.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim x As String 
   x = GetHostName("Rusty")
   Msgbox(x, "IP Address of host")
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 Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
I get :
LastException Java.net.nUnkownHostException: Unable to resolve host "Rusty": No address associated with hostname.
On my PC, I've run the following VB code
B4X:
lblHostName.Text = My.Computer.Name
Which verifies my host name is "Rusty"
What am I doing wrong?
Thanks,
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
Any idea what conditions may exist that cause the host name to not be resolvable?
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks for the post, Erel.
I created a "ping" program that will find any available socket. This could be enhanced to include a "handshake" to insure you've got the right connection, but I thought I'd share this. Please comment on improvements. thanks,
LIBRARIES: Core, Network
B4X:
''Activity module
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                        'A socket on which to connect
   Dim PingTimer As Timer                        'A timer to control the process
   Dim IPAddress As String                        'A string to hold our IP address
   Dim IPPort As String       : IPPort = "7890"      'I know this is the port on which he is listening
   Dim MachineNode As Int      : MachineNode = 0      
   Dim NetworkNode As Int      : NetworkNode = 1      
   Dim lblIPAddress As Label                     'just so we can watch what is going on.   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Activity.LoadLayout("IP")                  'we want to watch the lblIPAddress text
      PingTimer.Initialize("PingTimer", 100)         'I set the interval to .1 second, could be less
   End If
   PingTimer.Enabled = True                     'Ping timer will keep track of the time
End Sub

Sub Socket1_Connected (Successful As Boolean)         'if we get a connection then we are through
   PingTimer.Enabled = False
   If Successful Then
      Msgbox("We found our server" & IPAddress, "Success")
      Socket1.Close
      Activity.Finish
   Else
      PingTimer.Enabled = True                  'otherwise we re-enable the timer to try the next IP address
   End If
End Sub

Sub PingTimer_Tick
   PingTimer.Enabled = False
   MachineNode = MachineNode + 1
   If MachineNode < 255 Then                     'here we will increment and test the IP "machine" node
      IPAddress = "192.168." & NetworkNode & "." & MachineNode
      lblIPAddress.Text = "Trying " & IPAddress
      Socket1.Close
      Socket1.Initialize("Socket1")
      Socket1.Connect(IPAddress, IPPort, 0)
      PingTimer.Enabled = True
   Else                                    'if we have tried all 255 machine nodes, increment the "Network" node
      NetworkNode = NetworkNode + 1
      MachineNode = 0
      If NetworkNode >= 255 Then                  'alas, there is no socket available
         Msgbox("I could not find the server", "ERROR")
         Activity.Finish
      End If
   End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
In order for this to work, you must have a listener somewhere on your LAN.
 
Last edited:
Upvote 0

jeronimovilar2

Member
Licensed User
Longtime User
PING Test?

Hi Erel,
I´m using 5 android phone on my intranet (wifi router). it´s work fine, but sometimes they "lose" the connection during execution. Do you have or do you know a program to do the PING constantly so I know if my application or the network?

Sorry my english (again) :)
 
Upvote 0
Status
Not open for further replies.
Top