Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6882001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:12:00+00:00 2026-05-27T05:12:00+00:00

is there a way to get the IP number from gethostname() ? We are

  • 0

is there a way to get the IP number from gethostname()?

We are randomly generating IP addresses for the computers in the lab we are in. We use gethostbyname(<random ip>) to get the IP of a computer.

What we want to do essentially is compare the ip that we get from gethostbyname() with what we get from gethostname().

We tried:

struct hostent* host;
char temp[MAX_LEN];
gethostname(temp, MAX_LEN);

host = gethostbyname(<random ip address>)

if(host->h_name == temp) printf("They are the same\n");

The problem is, is that host->h_name is ‘172.125.45.1’ (i made that
up) and temp is ‘u-my_comp’

so we cant compare the strings cause one gives us the name of the computer (u-my_comp), and the other gives the ip…

Is there anyway to make these functions return the same type of value?

we have tried doing something like

gethostname(temp, 24)
temp_host = gethostbyname(temp)

in hopes that now we could compare temp_host->h_name with host->h_name…but yeah, that didnt work either.

Any ideas?

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:12:00+00:00Added an answer on May 27, 2026 at 5:12 am

    gethostbyname() is for converting a hostname into a socket address. If the “hostname” you supply is a dotted-quad IPv4 address, that will be all you get in the h_name parameter of the result.

    To convert a socket address back into a name what you want is the companion function gethostbyaddr() – except that you don’t, because both gethostbyname() and gethostbyaddr() are deprecated. Instead, you should be using getaddrinfo() and getnameinfo().

    For example:

    #include <stdio.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int main()
    {
        struct sockaddr_in sa;
        char host[1024];
        int gni_err;
    
        sa.sin_family = AF_INET;
        sa.sin_port = 0;
        sa.sin_addr.s_addr = inet_addr("127.0.0.1");
    
        gni_err = getnameinfo((struct sockaddr *)&sa, sizeof sa, host, sizeof host, NULL, 0, NI_NAMEREQD | NI_NOFQDN);
    
        if (gni_err == 0) {
            printf("host is: %s\n", host);
        } else {
            fprintf(stderr, "Error looking up host: %s\n", gai_strerror(gni_err));
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can get the actual row number from a query?
Is there any way to get input from a file one number at a
Is there any way to get own phone number by standard APIs from iPhone
Is there a way to get ISBN number from the title of the book
Is there a way to get the number of rows 'returned' from a stored
Is there a way to get the number of yield returns from within a
Is there any way to get the row id or row number from the
Is there any way to get the total number of rows returned from a
SELECT COUNT(*) FROM BigTable_1 Which way I should to use to get number of
Is there a way to get the last number from the range() function? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.