Domanda

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?

È stato utile?

Soluzione

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];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top