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 7694613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:21:33+00:00 2026-05-31T21:21:33+00:00

I have a a variable that contains an IP address. I am trying to

  • 0

I have a a variable that contains an IP address. I am trying to do an nslookup on that by instead of the DNS name being returned I get 0. I am in a Linux environment. Destination IP comes from a vector (string dest_ip = vector[2]).

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <vector>
#include <algorithm>
#include <iterator>
#include <sstream>

using namespace std;

void split(const std::string& str, std::vector<std::string>& v) {
    std::stringstream ss(str);
    ss >> std::noskipws;
    std::string field;
    char ws_delim;
    while(1) {
        if( ss >> field )
            v.push_back(field);
        else if (ss.eof())
            break;
        else
            v.push_back(std::string());
        ss.clear();
        ss >> ws_delim;
    }
}

int main()
{

  string input_line;

  while(cin){

  getline(cin, input_line);

   for(int i=0; input_line[i]; i++)
                      if(input_line[i] == ':') input_line[i] = ' ';
                     for(int i=0; input_line[i]; i++)
                      if(input_line[i] == '/') input_line[i] = ' ';

  std::vector<std::string> v;
  split(input_line, v);

  string dest_ip = v[4];

  struct hostent *he;
  int i,len,type;
  len = dest_ip.length();
  type=AF_INET;

  he = gethostbyaddr(dest_ip.c_str(),len,type);

  cout<<"Hostname: "<<he<<"\n";

  return 0;
}

Again, instead of receiving the host name I get 0.

  • 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-31T21:21:34+00:00Added an answer on May 31, 2026 at 9:21 pm

    You cannot pass a c-style-string (ie. null-terminated) directly to gethostbyaddr.

    You’ll need to create a struct in_addr and pass a pointer to the created struct as first parameter to gethostbyaddr. To generate a struct in_addr from a char const* use inet_aton.

    The below example is taken from man gethostbyaddr:


    EXAMPLES

    • Print out the hostname associated with a specific IP address:

      const char *ipstr = "127.0.0.1";
      struct in_addr ip;
      struct hostent *hp;
      
      if (!inet_aton(ipstr, &ip))
              errx(1, "can't parse IP address %s", ipstr);
      
      if ((hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET)) == NULL)
              errx(1, "no name associated with %s", ipstr);
      
       printf("name associated with %s is %s\n", ipstr, hp->h_name);
      

    How do I do further checks to pin-point what went wrong?

    If your use of gethostbyaddr returns NULL you should check what went wrong by looking at the variable h_errno.

    h_errno can have one of the below defined values:

    1. HOST_NOT_FOUND
    2. TRY_AGAIN
    3. NO_RECOVERY
    4. NO_DATA

    Please consult your manual for more details regarding the issue.



    Your snippet is completely wrong..

    The snippet provided by you doesn’t even compile, but you are in a way showing what you are trying to accomplish but I cannot know this for certain.
    This post contains details that should be considered to be “educated guesses“.

    OP changed his post..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Range variable that contains an address - $2:$2,$4:$205,$214:$214 - (3 groups
I have a variable that contains a 4 byte, network-order IPv4 address (this was
I have a variable that contains : Sometimes array of strings and sometimes array
I have a variable that contains the entire html for a page as a
I have a variable that contains an array from form data, seen below: $option1
If I have a variable that contains text information (say taken from a textarea),
I have a variable foo that contains a time, lets say 4pm today, but
I have a 'price' variable that contains some integer number from a MySQL database.
I have a Class named Constants that contains all the constant variable in my
I have a js file test.js that contains a global variable global_var and a

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.