Retrieving a string from a UDP server message

I am using the CocoaAsyncSocket library in my Swift based iOS application. I created an asynchronous UDP socket for a UDP server on my network and it sends a response.

I read this answer as follows:

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, this is a bunch of weird characters.

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

- , UDP-. , ASCII UDP "". , - , , Xcode. wþÀ¨r.

NSASCIIStringEncoding NSUTF8StringEncoding, EXC_BAD_ACCESS .

, Objective-C ( didReceiveData):

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

"" UDP ?

+1
3

, ! didReceiveData data: NSData! udpSocket .

, :

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

, .

let data = address

, - .

+2

, - , , . , "" ( ) ? Wireshark tcpdump?

, (16 ), , 3-7 IP-, , - , .

+1

All Articles