Katz
Katz

Reputation: 19

How to Resolve Hostnames of Devices on the Same Wi-Fi Network with NBNS Protocol?

I am currently attempting to resolve the hostname of a device connected to the same Wi-Fi network as mine. However, I've been unable to find sufficient resources or documentation on utilizing the NBNS (NetBIOS Name Service) protocol for this purpose. Can someone please guide how to achieve this?

I have tried GCDAsyncUdpSocket but no callbacks from the delegate.

var ip: String = "192.168.0.19"
var port: UInt16 = 137
let broadcastData: Data = "Hi".data(using: .utf8)!
var udpSocket : GCDAsyncUdpSocket!
func broadCast(){
    udpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: .main)  
    do {
        port = findFreePort()
        try udpSocket.bind(toPort: port)
        print("\nBind port \(port)")
        try udpSocket.enableReusePort(true)
        try udpSocket.enableBroadcast(true)
        print("Enable broadcast")
        try udpSocket.beginReceiving()
        print("Begin receiving")
        udpSocket.send(broadcastData, toHost: ip, port: port, withTimeout: -1, tag: 0)
        print("Send \(String(data: broadcastData, encoding: .utf8)!) to host \(ip) on port \(port)")
    } catch {
        print("udp broadcast error")
    }
}

Upvotes: 1

Views: 62

Answers (0)

Related Questions