title: Swift: Receive UDP with GCDAsyncUdpSocket
body: <p>BACKGROUND:</p>
  
  <p>I want to be able to send and receive UDP packets between my iOS app and a server.
  The server echoes back every incoming message to the client the app. <strong>The server is tested and confirmed working</strong>. I have a StartViewController which starting up two classes that implements GCDAsyncUdpSocketDelegate, one for sending and one for receiving. The "sending socket" is working, the server receives the messages.</p>
  
  <p>PROBLEM:</p>
  
  <p>The app never get the incoming message back after it been sent. Something with the listening socket setup is probably wrong since didReceiveData never get called.</p>
  
  <p>Have I done this completely wrong?</p>
  
  <p>Start:</p>
  
  <pre><code>class StartViewController: UIViewController {
  
     var inSocket : InSocket!
     var outSocket : OutSocket!
  
     override func viewDidLoad() {
         super.viewDidLoad()
         inSocket = InSocket()
         outSocket = OutSocket()
     }
  
     @IBAction func goButton(sender: UIButton) {
         outSocket.send("This is a message!")
     }
  }
  </code></pre>
  
  <p>Receive:</p>
  
  <pre><code>class InSocket: NSObject, GCDAsyncUdpSocketDelegate {
  
     let IP = "255.255.255.255"
     let PORT:UInt16 = 5556
     var socket:GCDAsyncUdpSocket!
  
     override init(){
         super.init()
         setupConnection()
     }
  
     func setupConnection(){
         var error : NSError?
         socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
         socket.bindToPort(PORT, error: &amp;error)
         socket.enableBroadcast(true, error: &amp;error)
         socket.joinMulticastGroup(IP, error: &amp;error)
         socket.beginReceiving(&amp;error)
     }
  
     func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!,      withFilterContext filterContext: AnyObject!) {
         println("incoming message: \(data)");
     }
  }
  </code></pre>
  
  <p>Send:</p>
  
  <pre><code>class OutSocket: NSObject, GCDAsyncUdpSocketDelegate {
  
     let IP = "90.112.76.180"
     let PORT:UInt16 = 5556
     var socket:GCDAsyncUdpSocket!
  
     override init(){
         super.init()
         setupConnection()
     }
  
     func setupConnection(){
         var error : NSError?
         socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
         socket.connectToHost(IP, onPort: PORT, error: &amp;error)
     }
  
     func send(message:String){
         let data = message.dataUsingEncoding(NSUTF8StringEncoding)
         socket.sendData(data, withTimeout: 2, tag: 0)
     }
  
     func udpSocket(sock: GCDAsyncUdpSocket!, didConnectToAddress address: NSData!) {
         println("didConnectToAddress");
     }
  
     func udpSocket(sock: GCDAsyncUdpSocket!, didNotConnect error: NSError!) {
         println("didNotConnect \(error)")
     }
  
     func udpSocket(sock: GCDAsyncUdpSocket!, didSendDataWithTag tag: Int) {
         println("didSendDataWithTag")
     } 
  
     func udpSocket(sock: GCDAsyncUdpSocket!, didNotSendDataWithTag tag: Int, dueToError error: NSError!) {
          println("didNotSendDataWithTag")
     }
  }
  </code></pre>
  
  <p><strong>Edit:</strong>
  Added forgotten code line.</p>