Network.framework badParam -65540

Hi Guys,


im trying deep dive into the new Network.framework. impossible to find any tutorials about...

So i wanted to test the example from apple which was shown on the WWDC2018
https://developer.apple.com/videos/play/wwdc2018/715/



But it's not working. has anyone tried out the example from the video ?

I implemented the UDP Server Class and during test, it's throwing the error badParam -65540 ?!



Has anyone a working example for me ?!



import Foundation
import Network

class UDPServer {
    var listner: NWListener
    var queue = DispatchQueue(label: "UDP.Server")
    required init() {
        self.listner = try! NWListener(using: .udp)
    }
    func start() {
        self.createListner()
        self.connectionHandler()
        self.stateHandler()
        self.listner.start(queue: self.queue)
    }
    func receive(on connection: NWConnection) {
        connection.receiveMessage { (data, context, isComplete, error) in
            if let _ = data {
                print("Received Data")
            }
            if error == nil {
                self.receive(on: connection)
            }
        }
    }
}

extension UDPServer {
    private func createListner() {
        self.listner.service = NWListener.Service.init(type: "_udp_.test")
        self.listner.serviceRegistrationUpdateHandler = { service in
            switch service {
            case .add(let endpoint):
                switch endpoint {
                case .service(let name, let type, let domain, let interface):
                    print("name:\(name), type:\(type), domain:\(domain), interface:\(String(describing: interface))")
                    break
                case .hostPort(_, _):
                    break
                case .unix(_):
                    break
                }
            case .remove(_):
                break
            }
        }
    }
    private func connectionHandler() {
        self.listner.newConnectionHandler = { connection in
            connection.start(queue: self.queue)
            self.receive(on: connection)
        }
    }
    private func stateHandler() {
        self.listner.stateUpdateHandler = { state in
            switch state {
            case .ready:
                print("connection ready, \(String(describing: self.listner.port))")
            case .failed(let error):
                print("failed start listening with error: \(error)")
            case .setup:
                break
            case .waiting(_):
                break
            case .cancelled:
                break
            }
        }
    }
}

Error -65540 is

kDNSServiceErr_BadParam
from
<dns_sd.h>
, which is a strong indication that this is related to service advertisement via Bonjour. And looking through the code snippet you posted I see the problem on line 30: You’re passing in a service type of
_udp_.test
, which is not correctly formatted. I suspect you want
_test._udp.
.

Note that service types are not specific to the Network framework. A good place to learn more about them is the Bonjour Overview document. The actual specification comes from RFC 2782, although RFC 6763 covers how they’re using in the Bonjour context.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Network.framework badParam -65540
 
 
Q