iosswiftudpcocoaasyncsocket

Retrieving a string from a UDP server message


I'm using the CocoaAsyncSocket library in my Swift-based iOS app. I have created an async UDP socket to a UDP server on my network, and it sends back a reply.

I'm reading this reply like this:

func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) { 
    println("didReceiveData")
    let data = address

    var host: NSString?
    var port1: UInt16 = 0
    GCDAsyncUdpSocket.getHost(&host, port: &port1, fromAddress: address)
    println("From \(host!)")

    let gotdata = NSString(data: data, encoding: NSASCIIStringEncoding)
    //let gotdata: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)
    println(gotdata)
}

This is the result:

didReceiveData
From 192.168.1.114
wþÀ¨r

As you can see, it's a bunch of strange characters.

The raw data is <100277fe c0a80172 00000000 00000000> (by writing println(data)). Here the first 4 bytes seem to be the wþÀ¨r part, and the next 4 are the IP address, which I already retrived from the getHost() function.

I can't seem to find any helper function to extract the actual data received by the UDP server. I know that the ASCII representation of the UDP reply is "hello". However, if I change this to something else, it is not reflected in the data I see in Xcode's debug part. It is always wþÀ¨r.

I have tried both with NSASCIIStringEncoding and NSUTF8StringEncoding, but the latter results in a EXC_BAD_ACCESS runtime exception.

As far as I could find, this is how people normally do it in Objective-C (inside the didReceiveData function):

NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

How do I find the "hello" message in the UDP reply as a string representation?


Solution

  • It is rather stupid, but removing ! from the didReceiveData data: NSData! part of the udpSocket function solved the issue.

    That is, the function should be defined like this:

    func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {
        ....
    }