0

I want to programmatically obtain the IP address of a computer on the LAN, given its hostname.

I know from the command line i can do this by ping < hostname >, but how can i do this programmatically in objective-c? Thanks.

jaamun
  • 181
  • 2
  • 12
  • can't you do a dns query? Not sure about objective C, but I know the regular C equivalent is getHostByName. – Wug Aug 13 '12 at 18:44
  • im not sure how to, but tat might be the way to go. can you offer any more guidance? – jaamun Aug 13 '12 at 20:02
  • you may find this question indirectly useful: http://stackoverflow.com/questions/5145895/gethostbyname-xcode-issues That said, I've never written a line of objective C, nor have I done any development on a mac or ios device. – Wug Aug 13 '12 at 20:05
  • thanks Wug! getHostByName works, but it seems to only work for websites like "google.com." Is there any way I can make it work to find an ip address of a computer on the LAN? Like an additional configuration setting or something? – jaamun Aug 14 '12 at 15:05
  • There is already a similar question - see [here](http://stackoverflow.com/questions/3572697/how-to-get-domain-name-of-ipaddress-and-ipaddress-from-domain-name-in-objective) (assuming you are using Mac OS). – Lyubomir Vasilev Aug 13 '12 at 18:43
  • that question shows how to find the name given an ip address. – jaamun Aug 13 '12 at 20:00
  • yes. and it does it using `getaddrinfo`. I guess Lyubomir fancied you could try a `man getaddrinfo` before rejecting his suggestion. – Analog File Aug 13 '12 at 20:53
  • ok, well, gethostbyname() for example works for website's ip addresses like "google.com," but it doesn't work for finding the ip address of a computer on your network like "bob's Mac" for instance. Is there an additional configuration that needs to be set up for this? – jaamun Aug 14 '12 at 15:03

1 Answers1

1

Ok, gethostbyname() always works when trying to find the ip address given a hostname, as long as the hostname of the computer you ae trying to access has its name registered in a DNS Server. All comments and answers above were useful.

jaamun
  • 181
  • 2
  • 12