Platforms to show: All Mac Windows Linux Cross-Platform
/Network/Bonjour/Find Computer iOS
Required plugins for this example: MBS Network Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Network/Bonjour/Find Computer iOS
This example is the version from Thu, 16th Dec 2020.
Project "Find Computer iOS.xojo_binary_project"
Class App Inherits MobileApplication
Property MainScreen As MainScreen
End Class
Class MainScreen Inherits MobileScreen
Control List Inherits iOSMobileTable
ControlInstance List Inherits iOSMobileTable
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
End Control
EventHandler Sub Opening()
app.MainScreen = Self
InitBonjour
End EventHandler
Sub AddDomain(type as string)
If Domains.IndexOf(type) < 0 Then
Domains.Append type
update
end if
End Sub
Sub AddType(type as string)
If ServiceTypes.IndexOf(Type) < 0 Then
list.AddSection type
ServiceTypes.Append type
update
end if
End Sub
Sub ClearAddrInfo()
dim u as integer = UBound(addrinfo)
for i as integer = u downto 0
if addrinfo(i).done then
addrinfo.Remove i
end if
next
End Sub
Sub ClearLookups()
dim u as integer = UBound(lookup)
for i as integer = u downto 0
if lookup(i).done then
lookup.Remove i
end if
next
End Sub
Sub InitBonjour()
// always search for those:
'serviceTypes.append "_ftp._tcp"
serviceTypes.append "_afpovertcp._tcp"
'serviceTypes.append "_nfs._tcp"
serviceTypes.append "_http._tcp"
serviceTypes.append "_printer._tcp"
'serviceTypes.append "_ichat._tcp"
'serviceTypes.append "_presence._tcp"
'serviceTypes.append "_daap._tcp"
'serviceTypes.append "_dpap._tcp"
serviceTypes.append "_ipp._tcp"
'serviceTypes.append "_pdl-datastream._tcp"
'serviceTypes.append "_distcc._tcp"
'serviceTypes.append "_xserveraid._tcp"
For Each serviceType As String In serviceTypes
list.AddSection serviceType
Next
browsers = new Dictionary
#if TargetMachO
m = new MyMetaQuery
#endif
MyDomainEnumeration = new MyDomainEnumeration
Call MyDomainEnumeration.EnumerateDomains(DNSServiceDomainEnumerationMBS.kFlagsBrowseDomains, DNSServiceDomainEnumerationMBS.kInterfaceIndexAny)
update
End Sub
Sub InitUDP()
dim u as new UDPSocket
u.port = 0
u.Connect
for each n as NetworkInterfaceMBS in NetworkInterfaceMBS.AllInterfaces
if n.Broadcast then
u.Write n.BroadcastAddress, "Hello"
end if
next
End Sub
Sub Report(ServiceName as string, RegType as string, domain as string, status as string, host as string = "", fullname as string = "")
fullname = fullname.ReplaceAll("\032", " ")
Dim SectionUbound As Integer = list.SectionCount-1
For SectionIndex As Integer = 0 To SectionUbound
Dim RowUbound As Integer = List.RowCount(SectionIndex) - 1
For RowIndex As Integer = 0 To RowUbound
Dim v As MobileTableCellData = list.RowCellData(SectionIndex, RowIndex)
Dim e As entry = v.tag
If e.domain = domain And e.RegType = RegType And e.ServiceName = ServiceName Then
// update this one
e.status = status
e.host = host
e.fullname = fullname
v.Text = domain + ServiceName
v.DetailText = status
list.ReloadRow(SectionIndex, RowIndex)
Return
end if
Next
next
// new entry
Dim e As New Entry
e.domain = domain
e.RegType = RegType
e.host = host
e.ServiceName = ServiceName
e.status = status
e.fullname = fullname
Dim value As MobileTableCellData = list.CreateCell(ServiceName + domain, status)
value.Tag = e
Dim section As Integer = ServiceTypes.IndexOf(RegType)
If section = -1 Then
// extra dot on end of name!?
RegType = RegType.Left(RegType.Length - 1)
section = ServiceTypes.IndexOf(RegType)
End If
List.AddRow section, value
End Sub
Sub StartAddrInfo(name as string, type as string, domain as string, HostName as string, FullName as string, Port as integer)
ClearAddrInfo
dim b as new MyAddrInfo
b.ServiceType = type
b.ServiceName = name
b.ServiceDomain = domain
b.FullName = FullName
b.port = port
if b.AddrInfo(b.kInterfaceIndexAny, b.kProtocolIPv4, HostName) then
addrinfo.Append b
end if
End Sub
Sub StartBrowse(domain as string, ServiceType as string)
dim key as string = ServiceType+domain
if browsers.HasKey(key) then Return
dim b as new MyBrowser
If b.Browse(b.kInterfaceIndexAny, ServiceType,domain) Then
b.type=ServiceType
b.domain = domain
browser.Append b
browsers.Value(key) = b
else
System.DebugLog "Failed to init browser for "+ServiceType
end if
End Sub
Sub StartLookup(name as string, type as string, domain as string)
ClearLookups
Dim b As New MyLookup
b.type = type
b.name = name
b.domain = domain
if b.Resolve(b.kInterfaceIndexAny, name, type, domain) then
lookup.Append b
end if
End Sub
Sub Update()
for each domain as string in domains
for each ServiceType as string in ServiceTypes
StartBrowse domain, ServiceType
next
next
End Sub
Property Domains() As string
Property MyDomainEnumeration As MyDomainEnumeration
Property ServiceTypes() As string
s
Property addrinfo() As MyAddrInfo
Property Protected browser() As MyBrowser
Property browsers As Dictionary
Property Protected lookup() As MyLookup
Property m As Variant
End Class
Class LaunchScreen Inherits iosView
End Class
Class MyBrowser Inherits DNSServiceBrowseMBS
EventHandler Sub ServiceBrowse(Flags as integer, InterfaceIndex as integer, ErrorCode as integer, ServiceName as string, RegType as string, Domain as string)
const kDNSServiceFlagsAdd=2
dim f as integer
f = Bitwise.BitAnd(Flags, kDNSServiceFlagsAdd)
Dim MainScreen As MainScreen = app.MainScreen
If f<>0 Then
// added
MainScreen.Report ServiceName, RegType, domain, "found"
MainScreen.StartLookup ServiceName, RegType, domain
else
// removed
MainScreen.Report ServiceName, RegType, domain, "gone"
end if
End EventHandler
Property domain As string
Property type As string
End Class
Class MyLookup Inherits DNSServiceResolveMBS
EventHandler Sub ServiceResolve(flags as integer, InterfaceIndex as integer, ErrorCode as integer, Fullname as string, Hosttarget as string, Port as integer, TxtRecord as string)
Dim MainScreen As MainScreen = app.MainScreen
if DNSServiceAddrInfoMBS.Available then
MainScreen.Report name, type, domain, "", Hosttarget, fullname
MainScreen.StartAddrInfo name, type, domain, Hosttarget, Fullname, port
else
MainScreen.Report name, type, domain, DNSNameToAddressMBS(Hosttarget)+":"+(port.tostring), Hosttarget, fullname
end if
done = (Bitwise.BitAnd(flags, Me.kFlagsMoreComing) = 0)
End EventHandler
Property domain As string
Property done As boolean
Property name As string
Property type As string
End Class
Class MyMetaQuery Inherits DNSServiceMetaQueryMBS
EventHandler Sub AddService(type as string, domain as string, interfaceName as string, rrtype as integer, rrclass as integer)
Dim MainScreen As MainScreen = app.MainScreen
MainScreen.AddType type
End EventHandler
End Class
Class Device
Property name As string
End Class
Class MyDomainEnumeration Inherits DNSServiceDomainEnumerationMBS
EventHandler Sub ServiceDomainEnumeration(flags as integer, interfaceIndex as integer, errorcode as integer, Domain as string)
System.DebugLog "Found domain: "+domain
Dim MainScreen As MainScreen = app.MainScreen
MainScreen.AddDomain domain
End EventHandler
End Class
Class MyAddrInfo Inherits DNSServiceAddrInfoMBS
EventHandler Sub ServiceAddrInfo(Flags as integer, InterfaceIndex as integer, ErrorCode as integer, AddressFamily as Integer, IP as string, SockAddr as MemoryBlock, HostName as string, ttl as Integer)
#If TargetMacOS
#pragma unused Flags
#pragma unused InterfaceIndex
#pragma unused ErrorCode
#pragma unused AddressFamily
#pragma unused IP
#pragma unused SockAddr
#pragma unused HostName
#pragma unused ttl
#EndIf
// we may get several calls per domain...
Dim MainScreen As MainScreen = app.MainScreen
MainScreen.Report ServiceName, ServiceType, ServiceDomain, IP+":"+(port.toString), Hostname, FullName
done = (Bitwise.BitAnd(flags, me.kFlagsMoreComing) = 0)
End EventHandler
Property Done As Boolean
Property FullName As string
Property ServiceDomain As string
Property ServiceName As string
Property ServiceType As string
Property port As Integer
End Class
Class Entry
Property RegType As string
Property ServiceName As string
Property Status As string
Property domain As string
Property fullname As string
Property host As string
End Class
End Project
See also:
The items on this page are in the following plugins: MBS Network Plugin.