سؤال

Does anyone know how the delegate method for receiving UDP data in CocoaAsyncSockets work when it comes to fetching the source address? Specifically the method

-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext

The address comes back as NSData* but interpreting it using NSUTF8StringEncoding returns null and NSASCIIStringEncoding returns a bunch of garbled characters. How is it supposed to be interpreted?

هل كانت مفيدة؟

المحلول

Figured out how to do it, the data is in the form of a struct sockaddr_in*. After importing <arpa/inet.h>you can do the following:

struct sockaddr_in *addr = (struct sockaddr_in *)[address bytes];
NSString *IP = [NSString stringWithCString:inet_ntoa(addr->sin_addr) encoding:NSASCIIStringEncoding];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top