2016-12-08 13 views
6

Próbuję utworzyć usługę Bonjour w mojej aplikacji na iOS, ale nie mogę jej opublikować. Tylko metoda delegatów netServiceWillPublish jest wywoływana, a usługa nie jest wyświetlana przy użyciu dns-sd.Problemy z Bonjour na iOS

Oto mój kod:

var service: NetService? = nil 

func start() { 
    createSockets() 

    service = NetService(domain: "test", type: "_test._tcp.", name: "Test", port: Int32(port)) 
    service?.delegate = self 
    service?.startMonitoring() 
    service?.publish() 
    service?.setTXTRecord(NetService.data(fromTXTRecord: [ 
     "model": "AppleTV3,2,1".data(using: .utf8)!, 
     "srcvers": "160.10".data(using: .utf8)!, 
     "features": "0x100009FF".data(using: .utf8)!, 
     "deviceId": "b8:53:ac:43:f3:15".data(using: .utf8)!, 
     "pw": "0".data(using: .utf8)!, 
     "rmodel": "MacBookPro10,2".data(using: .utf8)! 
     ])) 

} 

private func createSockets() { 
    ipv4Socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAutomaticallyReenableAcceptCallBack, socketCallback, nil) 

    var sin = sockaddr_in() 

    memset(&sin, 0, MemoryLayout<sockaddr_in>.size) 
    sin.sin_len = __uint8_t(MemoryLayout<sockaddr_in>.size) 
    sin.sin_family = sa_family_t(AF_INET); /* Address family */ 
    sin.sin_port = in_port_t(port) /* Or a specific port */ 
    sin.sin_addr.s_addr = INADDR_ANY 

    let sincfd = withUnsafePointer(to: &sin) { 
     $0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<sockaddr_in>.size) { 
      return CFDataCreate(kCFAllocatorDefault, $0, MemoryLayout<sockaddr_in>.size) 
     } 
    } 

    CFSocketSetAddress(ipv4Socket, sincfd) 

    let socketsource = CFSocketCreateRunLoopSource(
     kCFAllocatorDefault, 
     ipv4Socket, 
     0); 

    CFRunLoopAddSource(
     CFRunLoopGetCurrent(), 
     socketsource, 
     CFRunLoopMode.defaultMode); 
} 

Jestem nowy w tego typu sieci w iOS, co robię źle tutaj?

+2

Może spróbować utrzymując odwołanie swojego Netservice? Może został zwolniony. – koropok

+1

NetService ma odniesienie na poziomie klasy w kodzie, który nie działa. Po prostu zmieniłem go w poście, by oszpecić to, co robimy, ponieważ jest zastrzeżona. Zaktualizuję fragment kodu, więc to odzwierciedlaj. Dzięki. – Jake

+0

Używam podmiotu zewnętrznego obj-c do połączeń bonjour i działa poprawnie. Nie jestem pewien, czy to mogłoby pomóc. https://github.com/jdiehl/async-network – koropok

Odpowiedz

5

Używanie service?.publish(options: NetService.Options.listenForConnections) i nie tworzenie gniazda samodzielnie naprawia problem.

Doprowadzi to do opublikowania w ten sposób automatycznie utworzy słuchacza TCP dla obu IPv4 i IPv6 dla NetService.

Jabłko Dokumentacja:

/* When passed to -publishWithOptions:, in addition to publishing the service, a 
* TCP listener is started for both IPv4 and IPv6 on the port specified by the 
* NSNetService. If the listening port can't be opened, an error is reported using 
* -netService:didNotPublish:. Specify a port number of zero to use a random port. 
* When -netServiceDidPublish: is called, -port will return the actual listening 
* port number. Since the listener only supports TCP, the publish will fail with 
* NSNetServicesBadArgumentError if the NSNetService type does not end with "_tcp". 
* New incoming connections will be delivered in the form of NSStreams via the 
* -netService:didAcceptConnectionWithInputStream:outputStream: delegate method. 
*/ 
@available(iOS 7.0, *) 
public static var listenForConnections: NetService.Options { get } 
Powiązane problemy