Platforms to show: All Mac Windows Linux Cross-Platform

/Network/Bonjour/Find computer in Network


Required plugins for this example: MBS Network Plugin, MBS MacCF Plugin, MBS MacOSX Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Network/Bonjour/Find computer in Network

This example is the version from Sat, 9th Feb 2018.

Project "Find computer in Network.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class Window1 Inherits Window
Control List Inherits ListBox
ControlInstance List Inherits ListBox
End Control
EventHandler Sub Open() SetMaximumOpenFileCountMacOSXMBS 10000 InitBonjour 'InitUDP 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 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" 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 = ReplaceAll(fullname, "\032", " ") dim c as integer = List.ListCount-1 for i as integer = 0 to c if List.Cell(i,0) = domain then if List.Cell(i,1) = RegType then if List.Cell(i,2) = ServiceName then List.Cell(i,3) = status List.Cell(i,4) = host List.Cell(i,5) = fullname Return end if end if end if next List.AddRow domain, RegType, Servicename, status, host, fullname 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(0, 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(0,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) dim b as MyLookup ClearLookups b=new MyLookup b.type = type b.name = name b.domain = domain if b.Resolve(0,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
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
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=BitwiseAnd(Flags, kDNSServiceFlagsAdd) if f<>0 then // added Window1.Report ServiceName, RegType, domain, "found" window1.StartLookup ServiceName, RegType, domain else // removed Window1.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) if DNSServiceAddrInfoMBS.Available then Window1.Report name, type, domain, "", Hosttarget, fullname window1.StartAddrInfo name, type, domain, Hosttarget, Fullname, port else Window1.Report name, type, domain, DNSNameToAddressMBS(Hosttarget)+":"+str(port), Hosttarget, fullname end if done = (BitwiseAnd(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) window1.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 window1.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) // we may get several calls per domain... Window1.Report ServiceName, ServiceType, ServiceDomain, IP+":"+str(port), Hostname, FullName done = (BitwiseAnd(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
End Project

See also:

The items on this page are in the following plugins: MBS Network Plugin.


The biggest plugin in space...