如何获得AVAHI在iOS swift中发布的服务的IP和PORT?

时间:2017-02-10 06:12:52

标签: ios swift bonjour avahi nsnetservicebrowser

我正在使用NSNetServiceBrowser,并且能够在" didFindService"中找到AVAHI发布的服务列表。看起来像: 服务出现:本地。 _https._tcp。 TEMP-Mobileyes5-1C497B9ED382 -1 服务出现:本地。 _https._tcp。 TEMP-Mobileyes5-1C497B8E3916 -1 服务出现:本地。 _https._tcp。 TEMP-Mobileyes5-1C497B9ED380 -1

但无法找到相同服务的IP地址和端口号。我发现代码没有进入" netServiceDidResolveAddress"我的代码是:

class ServiceDiscovery : NSObject, NSNetServiceBrowserDelegate,NSNetServiceDelegate {

    var _browser:NSNetServiceBrowser!
    var _service: NSNetService!
    var services = [NSNetService]()
    override init() {
        _browser = NSNetServiceBrowser()
        super.init()
        _browser.delegate = self
        _browser.includesPeerToPeer = true
        _browser.searchForServicesOfType("_https._tcp.", inDomain: "local.")
        _browser.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    }


    func netServiceBrowser(browser: NSNetServiceBrowser, didFindDomain domainString: String, moreComing: Bool) {
        print(domainString)
    }

    func netServiceBrowser(aNetServiceBrowser: NSNetServiceBrowser, didFindService aNetService: NSNetService, moreComing: Bool) {
       print("Service appeared: \(aNetService)")
        services.append(aNetService)
        aNetService.delegate = self
        aNetService.resolveWithTimeout(5.0)
    }


    func netServiceBrowser(browser: NSNetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
        print(errorDict)
    }

    func netServiceBrowser(browser: NSNetServiceBrowser, didRemoveService service: NSNetService, moreComing: Bool) {
        print("Service removed: \(service)")

    }

    func netService(sender: NSNetService, didNotResolve errorDict: [String : NSNumber]) {
        print(errorDict)
    }

    func netServiceDidResolveAddress(sender: NSNetService) {
        print(sender.addresses![0])
    }


}

2 个答案:

答案 0 :(得分:1)

您遇到问题的可能原因可能是您正在调用

aNetService.resolveWithTimeout(5.0)

在另一个函数中,所以只要

didFindService

结束,本地变量 aNetService 被破坏(因为它是didFindService函数的局部变量)

<强>解

我看到您已经在顶部定义了一个变量,其中的类范围名为&#34; _service&#34; 因此,使用

_service = aNetService
_service.resolveWithTimeout(5.0)

didFindService 中 这应该可以解决你的问题。而且, netServiceDidResolveAddress 现在应该被调用。

答案 1 :(得分:0)

import Foundation
class ServiceDiscovery : NSObject, NetServiceBrowserDelegate,NetServiceDelegate {

    var _browser:NetServiceBrowser!
    var _service: NetService!
    var services = [NetService]()


    func searchServices(){
        self.services.removeAll()
        _browser = NetServiceBrowser()
        _browser.delegate = self
        _browser.includesPeerToPeer = true
        _browser.searchForServices(ofType: "_https._tcp.", inDomain: "local.")
        _browser.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
    }


    func updateInterface () {
        for service in self.services {
            if service.port == -1 {
                print("service \(service.name) of type \(service.type)" +
                    " not yet resolved")
                service.delegate = self
                service.resolve(withTimeout: 0.0)
            } else {

                let deviceLanController = DeviceLanController()
                let dict = NetService.dictionary(fromTXTRecord: service.txtRecordData()!)
                let id = self.copyStringFromTXTDict(dict as [AnyHashable: Any]?, which: "id")
                var ipAdd = ""
                if let address = service.addresses{
                if let addressOfFirstDevice = address.first{
                    let theAddress = addressOfFirstDevice as Data
                    var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                    if getnameinfo((theAddress as NSData).bytes.bindMemory(to: sockaddr.self, capacity: theAddress.count), socklen_t(theAddress.count),
                                   &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
                        if let numAddress = String(validatingUTF8: hostname) {
                            ipAdd = numAddress
                        }
                    }

                    if let serviceId = id{
                        deviceLanController.setDeviceAvailable(serviceId, host: ipAdd, port: "\(service.port)")
                    }
                }
                }
            }
        }
    }


    fileprivate func copyStringFromTXTDict(_ dict: [AnyHashable: Any]?, which: String) -> String? {
        // Helper for getting information from the TXT data
        var resultString: String? = nil
        if let data = dict?[which as NSObject] as! Data? {
            resultString = String(data: data, encoding: String.Encoding.utf8)!

        }
        return resultString
    }



    func netServiceBrowser(_ browser: NetServiceBrowser, didFindDomain domainString: String, moreComing: Bool) {
        print("didFindDomain")
        print(domainString)
    }

    func netServiceBrowser(_ aNetServiceBrowser: NetServiceBrowser, didFind aNetService: NetService, moreComing: Bool) {
        print("didFindService")
        self.services.append(aNetService)
        if !moreComing {
            aNetService.stop()
            self.updateInterface()
        }
    }

    func netServiceBrowser(_ browser: NetServiceBrowser, didRemove service: NetService, moreComing: Bool){
        print("didRemoveService")
        if let ix = self.services.index(of: service) {
            self.services.remove(at: ix)
            print("removing a service")
            if !moreComing {
                self.updateInterface()
            }
        }
    }

    func netServiceBrowser(_ browser: NetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
        print("didNotSearch")
        print(errorDict)
    }



    func netService(_ sender: NetService, didNotResolve errorDict: [String : NSNumber]) {
        print("didNotResolve",sender)
        print(errorDict)

    }

    func netServiceWillResolve(_ sender: NetService) {
        print("netServiceWillResolve",sender)
    }
    func netServiceDidResolveAddress(_ sender: NetService) {
        print("netServiceDidResolveAddress",sender)
       self.updateInterface()
    }

}

这个答案解决了我的问题。我正在解析并保存我的coredata中的每个地址,以便我可以使用该地址连接本地服务。