inet_addr & gethostbyaddr()

This is a discussion on inet_addr & gethostbyaddr() within the C++ Programming forums, part of the General Programming Boards category; I am new. Hello! I am trying to load in a text file that has IP addresses on each line. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    Unhappy inet_addr & gethostbyaddr()

    I am new. Hello!

    I am trying to load in a text file that has IP addresses on each line. This should return to the screen the reverse lookup on each address.

    What am I doing wrong? It compiles fine, but when I run it, it tells me the first address could not resolve, and then the second address shows "Host: " then hangs and gives a Windows error.

    The IP's in question are ->

    69.31.48.25
    139.146.133.180

    Code:
    #include <iostream>
    #include <winsock.h>
    #include <fstream>
    
    int main()
    {
      using namespace std;
      WSAData wData;
    
      if (WSAStartup(MAKEWORD(2,2), &wData) == SOCKET_ERROR)
      {
        cout << "Winsock init error\n";
        return 1;
      }
    
      hostent *h = NULL;
      const char *ip;
      string line;
    
      ifstream myfile("text.txt");
    	  if (myfile.is_open())
    	  {
    	      while (! myfile.eof() )
    	      {
    		getline (myfile,line);
    		ip = line.c_str();
                    unsigned int addr;
                    addr = inet_addr(ip);
            
                    h = gethostbyaddr(reinterpret_cast<char *>(&addr), 4, AF_INET);
                    if (h == NULL)
                    {
                     cout << "Could not resolve address" << endl;
                     //return 1;
                    }
                    cout << "Host: " << h->h_name << endl;		  
                 }
              }
    
    
      return 0;
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
                    h = gethostbyaddr(reinterpret_cast<char *>(&addr), 4, AF_INET);
                    if (h == NULL)
                    {
                     cout << "Could not resolve address" << endl;
                     //return 1;
                    }
                    cout << "Host: " << h->h_name << endl;
    I think what actually happens is that gethostbyaddr fails, and then you say you couldn't resolve it, and then continue to try to dereference the null pointer and print the name afterwards (because that return 1 is commented out). Try to handle the error in a different way (also make sure to use wsagetlasterror and wsacleanup)

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 10:22 AM
  2. gethostbyaddr is slow
    By eth0 in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-18-2005, 05:16 AM
  3. gethostbyaddr( )
    By xlordt in forum Networking/Device Communication
    Replies: 4
    Last Post: 10-06-2003, 09:25 PM
  4. gethostbyaddr
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 10-15-2001, 10:08 AM
  5. gethostbyaddr Help Plz
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 08-30-2001, 06:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21