ios - cfhoststartinforesolution fails. get device name from IP address on LAN -
as part of assignment have scan lan , names of devices connected wifi. went through apples documentation this , many other s.o links this. resolution not happening, error occurs. not networking guy , hence of sounded greek me. same question has been asked here code. have written down error occurring in comments.
an alternate answer helpful too.
+ (nsarray *)hostnamesforaddress:(nsstring *)address { // host reference given address. cfstreamerror streamerror; struct addrinfo hints; struct addrinfo *result = null; memset(&hints, 0, sizeof(hints)); hints.ai_flags = ai_numerichost; hints.ai_family = pf_unspec; hints.ai_socktype = sock_stream; hints.ai_protocol = 0; int errorstatus = getaddrinfo([address cstringusingencoding:nsasciistringencoding], null, &hints, &result); if (errorstatus != 0) return nil; cfdataref addressref = cfdatacreate(null, (uint8 *)result->ai_addr, result->ai_addrlen); if (addressref == nil) return nil; freeaddrinfo(result); cfhostref hostref = cfhostcreatewithaddress(kcfallocatordefault, addressref); if (hostref == nil) return nil; cfrelease(addressref); bool issuccess = cfhoststartinforesolution(hostref, kcfhostnames, &streamerror); // false if (!issuccess){ nslog(@"error:%@",[self convertcfstreamerrorintonserror:streamerror]); // error:error domain=kcferrordomaincfnetwork code=2 "the operation couldn’t completed. (kcferrordomaincfnetwork error 2.)" userinfo=0x1cd29190 {kcfgetaddrinfofailurekey=8} return nil; } // hostnames host reference. cfarrayref hostnamesref = cfhostgetnames(hostref, null); nsmutablearray *hostnames = [nsmutablearray array]; (int currentindex = 0; currentindex < [(__bridge nsarray *)hostnamesref count]; currentindex++) { [hostnames addobject:[(__bridge nsarray *)hostnamesref objectatindex:currentindex]]; } return hostnames[0];
}