0

I can get the Domain Name easily using 'Environment.UserDomainName' in .NET, but how can I find it out on objective c? I'm developing a small cocoa application for Mac OS using Xcode 6.

My pc will be connected to an Organization's LAN, i need to know the domain name of the network i connected to. I have tried the below method utilising NSHost class to get the hostname when the IP is provided. I passed the IP of pc, but it returned the IP not the hostname with Domain.

    -(void)GetHostName: (char*)DNSIP
  {
    int error;
    struct addrinfo hints;
    struct addrinfo *results = NULL;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;        
    hints.ai_protocol = IPPROTO_TCP;


    getaddrinfo(DNSIP, NULL, &hints, &results);
    for (struct addrinfo *r = results; r; r = r->ai_next)
    {
        char hostname[NI_MAXHOST] = {0};
        error = getnameinfo(r->ai_addr, r->ai_addrlen, hostname, sizeof hostname, NULL, 0 , 0);
        if (error != 0)
        {
            continue; // try next one
        }
        else
        {
            NSLog (@"Found hostname: %s", hostname);

            break;
        }
    }
    freeaddrinfo(results);

}

A screenshot is also attached to see what i needed actually through objective c. Any help on this will be greatly appreciated.

screenshot of network settings

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
  • Welcome to SO, please be a bit more specific when asking question: what have you tried, what do you expect, etc. See [how to ask](http://stackoverflow.com/help/how-to-ask) – Nehal Jan 27 '16 at 05:48
  • hi @Nehal, I hope i have specified what i need exactly with the help of screen shot. I have tried the below method utilizes NSHost class. But we have to provide IP of DNS server and it returns the full HostName of server. I have tried the method by passing IP of my own pc, but it returns the same ip, not the complete host name with domain. I have edited my post to include the method i have tried. – Jacob Chacko Jan 27 '16 at 06:26

0 Answers0