title: Implementing A UDP "Listener" In Swift?
body: <p>I've been trying to somewhat reverse engineer a project to discover Logitech Harmony Hub devices on my network, and posted <a href="https://stackoverflow.com/questions/35351806/udp-broadcast-device-discovery-on-ios">this</a> question to see if someone could help me understand UDP broadcast.  The answer explained that I've implementing the <em>send</em> portion of the UDP broadcast, but I've not implemented anything to "listen" for responses.  And that's where I'm struggling.  Here's my <em>send</em> code;</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.enableBroadcast(true)
      } 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>I see I have the code to handle the response (in <code>didReceiveData</code>), but I'm unsure what I need to implement to get the listening going;</p>
  
  <ul>
  <li>Do I need to "bind" to the listener port (in this case, 61991)?</li>
  <li>Do I need to "join the multicast group"?  And if so, at what address?  I tried doing so at "255.255.255.255", which creates a <code>setSocketOpt()</code> error when I build.</li>
  <li>I know I need to call <code>beginReceiving()</code>, and can I do all this on <code>socket</code>, or do I need to instantiate a separate socket for the listening?</li>
  </ul>
  
  <p><strong>Edit: Resolved</strong></p>
  
  <p>The below answer absolutely helped me to solve the problem.  It seemed I wasn't getting a response because I had not fully implemented a means of handling the incoming response.</p>
  
  <p>Per the code provided in the answer below, I added the following;</p>
  
  <pre><code>// Setup the other socket (used to handle the response from the Harmony hub)
      otherSocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
  
      do {
  
          // Accept connections on port 61991
          try otherSocket.acceptOnPort(61991)
  
      } catch {
  
          // Handle any errors here
          print(error)
      }
  </code></pre>
  
  <p>I also set this controller to be a <code>GCDAsyncSocketDelegate</code>, which seemed to do the trick.  I was able to read the response in <code>didReadData</code>.</p>