iOS Question B4i Wifi Sniff?

Yvon Steinthal

Active Member
Licensed User
Hi all, my title may confuse and im sorry for that!

I have succesfully created an iOS app used to Remote an Android App using:

https://www.b4x.com/android/forum/threads/b4x-network-asyncstreams-b4xserializator.72149/

And thats awesome!

However i would like to bypass the IP input by a way to sniff for potential candidates (There may be more than 1 Android App requesting to be remotely controlled via Wifi)

So far i've done something like this:

B4X:
    Dim server As ServerSocket
    Dim myip As String = server.GetMyIP
    Dim subip As String = myip.SubString2(0,myip.LastIndexOf(".")+1)
    Dim lastip As Int = 255
    
    isSearching = True
        
    For i=0 To lastip Step 1
        
        Dim newip As String = subip&i
        ConnectToServer(newip)
        Sleep(150)
        
    Next

And honestly i am almost 100% positive its not the right way to go! I find it long and unreliable...

Any ideas?

Thanks

Y.
 

Sandman

Expert
Licensed User
Longtime User
Well, something like this should probably work:
  • Server broadcasts a short UDP message on the LAN. The message contains a unique identifier for your app, together with the IP, and perhaps name, of the server. Any client who gets this message could present to the user "Connect to <name>?"

However, please note that not all LANs handle broadcast messages (my router doesn't, which is annoying for me), which means that you also need to have a backup plan, and that can be as simple as this:
  • When server is not connected to client, server displays its IP number. The client allows for manual input of IP number.

Regarding broadcasting, that's a special thing in networking. Read about it here, and search the forum here.

In any event, this is something that Erel (and probably many users in the forum) knows lots and lots about. What I described above isn't super far from how it works between B4X and the Bridge.
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
To do a follow up on this,

I have successfully made a "Wifi Sniffer" using Sandman's idea using UDP broadcasting. I have used a service on my B4a App to send every second a broadcast with its own IP as a message. The B4i app listens to this on a special port and receives all broadcasts in a Map (that way there are no doubles or triples due to the 2-3 seconds of listening). And voila...

I can post code if anyone is interested.

Y.
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
I am interested in your code please Sir.
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
My project was to sniff from iPad or iPhone any devices in my wifi network to be able to list them and eventually connect to them.
I will talk about the iPhone as my "Sniffing" device, and my Android Tablet as the one sending UDP packets.

I did help myself with UDP Chat created by Erel, i can't find the url anymore though...


I hope this helps...

B4X:
'This sub will start listening to UDP packets for 2 seconds
'This part is on the iPhone device

'Dim autodiscover as UDPSocket
'Dim mapDevices as Map

Sub ListenForPackets
   
    hd.ProgressDialogShow("Finding devices on your network...")
   
    LocalDeviceList.Clear
    LocalDeviceList.Initialize
    mapDevices.Initialize

    autodiscover.Initialize("AutoDiscover",51042,8000)
    Sleep(2000)
    autodiscover.Close

    hd.ProgressDialogHide
   
    If(mapDevices.Size>0)Then
        'BuildList

    Else
        hd.ToastMessageShow("No Devices found on local network!",True)   
    End If
End Sub

Private Sub AutoDiscover_PacketArrived (Packet As UDPPacket)
   
    Try
        Dim bc As ByteConverter
        Dim data(Packet.Length) As Byte
        bc.ArrayCopy(Packet.Data, Packet.Offset, data, 0, Packet.Length)
        Dim ds As String = serializator.ConvertBytesToObject(data)
        Log(ds)
        If(ds.Contains("/"))Then
           
            Dim name As String = ds.SubString2(0,ds.IndexOf("/"))
            Dim ip As String = ds.SubString2(ds.IndexOf("/")+1,ds.LastIndexOf("/"))
            Dim mac As String = ds.SubString(ds.LastIndexOf("/")+1)
       
        End If
       
        Dim tmap As Map
        tmap.Initialize
        tmap.Put("ip",ip)
        tmap.Put("name",name)
       
        mapDevices.Put(mac,tmap)
       
    Catch
        Log(LastException)
    End Try
   
End Sub
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
On the android side, sending UDP Packets i created a Service to Broadcast IP and other info i needed...

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private autodiscover As UDPSocket
    Private workingUDP As Boolean = True
    Private port As String = 51042
    Private serializator As B4XSerializator
    Private BroadcastTimer As Timer
    Private server As ServerSocket
   
End Sub

Sub Service_Create
    autodiscover.Initialize("autodiscover",port , 8192)
    BroadcastTimer.Initialize("BroadcastTimer", 1000)
   
End Sub

Sub Service_Start (StartingIntent As Intent)
    BroadcastTimer.Enabled = True
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
   
End Sub

Sub Service_Destroy
   
   
   
End Sub

Private Sub BroadcastTimer_Tick
   
    Dim address As String = GetBroadcastAddress
    If address <> "" Then
        Dim up As UDPPacket
        up.Initialize(serializator.ConvertObjectToBytes(Main.DeviceName&"/"&server.GetMyWifiIP&"/"&Main.EncodedMac), address, port)
        autodiscover.Send(up)
    End If
   
End Sub

Private Sub GetBroadcastAddress As String
    Dim niIterator As JavaObject
    niIterator = niIterator.InitializeStatic("java.net.NetworkInterface").RunMethod("getNetworkInterfaces", Null)
    Do While niIterator.RunMethod("hasMoreElements", Null)
        Dim ni As JavaObject = niIterator.RunMethod("nextElement", Null)
        If ni.RunMethod("isLoopback", Null) = False Then
            Dim addresses As List = ni.RunMethod("getInterfaceAddresses", Null)
            For Each ia As JavaObject In addresses
                Dim broadcast As Object = ia.RunMethod("getBroadcast", Null)
                If broadcast <> Null Then
                    Dim b As String = broadcast
                    Return b.SubString(1)
                End If
            Next
        End If
    Loop
    Return ""
End Sub
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
The service never dies really, because i want other devices to be able to sniff that device out !
This code is obviously not 100% fullproof, it works for me and i did very little debugging so far.
In any case hope it helps someone!
 
Upvote 0
Top