Android Question LAN device Name

pauleffect

Member
Licensed User
Longtime User
Hello.

I'm developing an app that needs to connect to a PC server over LAN. The easy way is, of course, having the user type an IP and port and voila...
But I'd like to find a way to keep user input (regarding networking) to a minimum.
So, instead of manually typing IP's, I used
B4X:
    p.Shell("cat /proc/net/arp", Null, sb, Null)
which outputs
B4X:
bla bla
192.168.0.129    0x1         0x2         d0:57:7b:a7:80:dc     *        wlan0
bla bla

Now, the IP part is taken care of, but do you guys know how I can find the machine's name (for example, my PC's called Slave, my laptop MobileSlave etc... )? Is this part possible?

I did find a way posted somewhere around here, namely

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

But this seems to simply return the same IP I input.

Any thoughts?
 

pauleffect

Member
Licensed User
Longtime User
Thank you for the suggestion.

I looked at your example, but I already decided upon the necessity of a tcp server (c#) adding an UDP layer just for autoconnection seemed overkill. So I hardcoded five ports and added them to a portList, while an ipList held the results of the p.Shell call detailed above.

After that, I simply call
B4X:
 socket1.Connect(ipList.Get(0), portList.Get(0), 500)

which leads to

B4X:
'based on erels async streams tutorial, ofc
Private Sub socket1_Connected (Successful As Boolean)
 'client connected to server
 If Successful Then
  WifiConnected = True
  Log("found")
  StartAStream(socket1.InputStream, socket1.OutputStream)
 Else
  WifiConnected = False
  If(ipListIndex < ipList.Size-1)Then
   ipListIndex = ipListIndex + 1
   socket1.Initialize("socket1")
   socket1.Connect(ipList.Get(ipListIndex), portList.Get(portListIndex), 1000)
  Else
   If(portListIndex<portList.Size -1)Then
    portListIndex = portListIndex + 1
    ipListIndex = 0
    socket1.Initialize("socket1")
    socket1.Connect(ipList.Get(ipListIndex), portList.Get(portListIndex), 1000)
   Else
    ToastMessageShow ("Network fatality. Please insert another coin.", True)
   End If
  End If
 End If
End Sub

So, basically, socket1 tries to connect to every ip-port possible combination of ip-port. And it's pretty fast, as it only takes a couple of seconds.

After I'm tidying up the server part, I'll post the C# code, just in case someone needs it.
 
Upvote 0
Top