Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Experiment 4: Information About Host, Network, Protocols & Domains

Lesson 4 of 11 in the free Network Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Program Statement

Write a program that retrieves (a) host information, (b) network information, (c) the protocol table and (d) the local domain name.

/* lookup.c — (a) host (b) network (c) protocols (d) domain */
int main(int argc, char **argv) {
    struct hostent *h = gethostbyname(argv[1]);            /* (a) host   */
    if (h) {
        printf("official name: %s\n", h->h_name);
        for (char **p = h->h_aliases; *p; p++) printf("alias: %s\n", *p);
        for (char **p = h->h_addr_list; *p; p++) {
            char ip[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, *p, ip, sizeof(ip));
            printf("address: %s\n", ip);
        }
    }
    struct protoent *pr;                                    /* (c) protocols */
    while ((pr = getprotoent()) != NULL)
        printf("protocol %-12s number %d\n", pr->p_name, pr->p_proto);
    endprotoent();

    struct servent *se = getservbyname("http", "tcp");      /* services      */
    printf("http/tcp is port %d\n", ntohs(se->s_port));

    char domain[256];                                       /* (d) domain    */
    getdomainname(domain, sizeof(domain));
    printf("NIS/local domain: %s\n", domain[0] ? domain : "(none)");
    /* (b) network: getnetbyname("loopback") — largely historical (/etc/networks) */
    return 0;
}

Theory Link

Unit 3 "Name & Address Conversions".

Extension

Redo part (a) with the modern, protocol-independent getaddrinfo and compare the two APIs in your record — gethostbyname is IPv4-only and not thread-safe; getaddrinfo handles both IPv4 and IPv6.