How to get the TCP client data by nw_connection_receive?

I'm using Network framework to do a simple TCP server.
When the TCP client send the data to me, nw_connection_receive handler, it give a parameter content which its type is dispatch_data_t.

I really no idea how to get the client's data from dispatch_data_t.

    nw_connection_receive(connection, 1, 256, ^(dispatch_data_t _Nullable content, nw_content_context_t _Nullable context, bool is_complete, nw_error_t _Nullable error) {

      // I want to get the data which client send to me from dispatch_data_t content.  

    });

Anyway, I did hate dispatch_data_t!!!!!!!!
Answered by Systems Engineer in 663731022
Have you tried casting dispatch_data_t to NSData and then using the byte array?

Code Block ObjC
if (is_complete && content != nil) {
NSData* bufferData = (NSData*)content;
/* Do something with [bufferData bytes] here*/
}



Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Accepted Answer
Have you tried casting dispatch_data_t to NSData and then using the byte array?

Code Block ObjC
if (is_complete && content != nil) {
NSData* bufferData = (NSData*)content;
/* Do something with [bufferData bytes] here*/
}



Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Hi Matt,

Yes, the method you provided, it works well.
Much thanks to help me, I spent the whole night for this.

Thanks
Jarvis
How to get the TCP client data by nw_connection_receive?
 
 
Q