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;
}