title: UDP Broadcast/Device Discovery on iOS?
body: <p>I am working on trying develop a means of discovering Logitech Harmony Hub devices on my local network, from an iOS app.  The concept is inspired by <a href="https://github.com/swissmanu/harmonyhubjs-discover" rel="noreferrer" title="harmonyhubjs-discover">this</a> NODE.JS project, which seems to send out a UDP broadcast to the <code>255.255.255.255</code> address, and then procures the Logitech's IP address (which is all I'm after).  When testing the NODE.JS project on my home network from my Mac, it successfully finds the Logitech Harmony Hub.</p>
  
  <p>I am using <code>CocoaASyncSocket</code>, and must admit, my understanding of how UDP broadcast/discovery works may be askew here.  Here's what I'm doing;</p>
  
  <pre><code>import UIKit
  import CocoaAsyncSocket
  
  class ViewController: UIViewController, GCDAsyncUdpSocketDelegate {
  
  var address = "255.255.255.255"
  var port:UInt16 = 5224
  var socket:GCDAsyncUdpSocket!
  var socketReceive:GCDAsyncUdpSocket!
  var error : NSError?
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      let message = "_logitech-reverse-bonjour._tcp.local.\n61991".dataUsingEncoding(NSUTF8StringEncoding)
  
      socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
      socket.sendData(message, toHost: address, port: port, withTimeout: 1000, tag: 0)
  
      do {
          try socket.bindToPort(port)
      } catch {
          print(error)
      }
  
      do {
          try socket.enableBroadcast(true)
      } catch {
          print(error)
      }
  
      do {
          try socket.beginReceiving()
      } catch {
          print(error)
      }
  
  }
  
      func udpSocket(sock: GCDAsyncUdpSocket!, didConnectToAddress address: NSData!) {
          print("didConnectToAddress");
      }
  
      func udpSocket(sock: GCDAsyncUdpSocket!, didNotConnect error: NSError!) {
          print("didNotConnect \(error)")
      }
  
      func udpSocket(sock: GCDAsyncUdpSocket!, didSendDataWithTag tag: Int) {
          print("didSendDataWithTag")
      } 
  
      func udpSocket(sock: GCDAsyncUdpSocket!, didNotSendDataWithTag tag: Int, dueToError error: NSError!) {
          print("didNotSendDataWithTag")
      }
  
  func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {
  
      var host: NSString?
      var port1: UInt16 = 0
      GCDAsyncUdpSocket.getHost(&amp;host, port: &amp;port1, fromAddress: address)
      print("From \(host!)")
  
      let gotdata: NSString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
      print(gotdata)
  
  }
  
  }
  </code></pre>
  
  <p>When I compile this, the only response I get is the message I just sent out;</p>
  
  <pre><code>didSendDataWithTag
  From ::ffff:192.168.1.101
  _logitech-reverse-bonjour._tcp.local.
  61991
  From 192.168.1.101
  _logitech-reverse-bonjour._tcp.local.
  61991
  </code></pre>
  
  <p>I fear that I have a conceptual understanding issue with the broadcast here, and am sincerely hoping that someone may be able to point me to a resource or help to understand why I'm not getting any response from the device in my code.</p>
  
  <p>Thanks!</p>