#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
usingnamespace std;
string getip(string hostname)
{
//function get ip
}
void main ()
{
string string_main="http://abc-xyz.com/www/~av.50.22.100.250?url.aspx/etc";
int count=0;
string string_sub;
ifstream infile;
infile.open ("E:\\test.txt");
while(!infile.eof()) // To get you all the lines.
{
getline(infile,string_sub); // Saves the line in string_sub.
// cout<<string_sub<<"\n"; // Prints our string_sub.
if (!string_sub.empty())
{
string temp=getip(string_sub); // get ip of string_sub on the text line by line
size_t result = string_main.find( temp ); //check string_sub on the text file line by line, comparable to string_main
if( result != string::npos ) // if found
{
count+=1;
}
}
}
infile.close();
//cout<< count <<"\n";
if (count != 0)
{
cout << "Found\n";
}
else
{
cout << "Not found\n";
}
getch();
}
Hi kbw
This link at linux. I test in MVSC++ 2010 Pro .New project -> C++ -> WIndow 32 console apllication -> Empty project .Then , i add file .CPP in solution and code
Can you demo in my code . Thank
Hi kbw
I have link library string_sub of project . After read two link reference . I try it . I try convert String to char , but not convert , Can you see it .Thank
You convert a string to a char like this hostname.c_str() but you need to pass a constant char pointer to the "gethostbyname()" function so reference the first character in the string like this &hostname[0]
Are you trying ip address from hostname or the hostname from ip address?
Only call WSAStartup once near the beginning of your program.
Why is this global?
HOSTENT* list_ip;
In general, you should resist the temptation to use global variables.
This:
list_ip=gethostbyname((char)hostname); // convert string to char
should probably be:
list_ip = gethostbyname(hostname.c_str());
Also, this:
1 2 3 4 5 6
ifstream infile;
infile.open ("E:\\test.txt");
while(!infile.eof()) // To get you all the lines.
{
getline(infile,string_sub); // Saves the line in string_sub.
should be:
1 2 3
ifstream infile("E:\\test.txt");
while (getline(infile, string_sub))
{
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32")
WSADATA wsaData; //golbal
//HOSTENT* list_ip; golbal
IN_ADDR addr; //golbal
usingnamespace std;
string getip(string hostname)
{
HOSTENT* list_ip; // not golbal, in this function
/*
// convert string to char
char c[1000];
int so=hostname.size();
for(int a=0;a<=so;a++)
{
c[a]=hostname[a];
}
*/
WSAStartup(MAKEWORD(2,0),&wsaData);
//list_ip=gethostbyname(c);
list_ip=gethostbyname(hostname.c_str()); //convert hostname from string to char
if (list_ip ==NULL)
{
ExitProcess(0);
}
memcpy(&addr.S_un.S_addr , list_ip->h_addr, list_ip->h_length);
return inet_ntoa(addr);
WSACleanup();
}
void main ()
{
//string string_main="http://abc-xyz.com/www/~av.50.22.100.250?url.aspx/etc";
//int count=0;
string string_sub;
ifstream infile("E:\\test.txt");
while(!infile.eof()) // To get you all the lines.
{
getline(infile,string_sub); // Saves the line in string_sub.
cout<<string_sub<<"\n"; // Prints our string_sub.
if (!string_sub.empty()) //check line have content
{
string temp=getip(string_sub); //temp get ip from string_sub
cout <<temp<<"\n"; //prints IP of hostname
//size_t result = string_main.find( string_sub ); //check string_sub on the text file line by line, comparable to string_main
//if( result != string::npos ) // if found
//{
// count+=1;
// //cout << "String is found at position"<< infile.tellg() << "\n";
//}
}
}
infile.close();
/*cout<< count <<"\n";
if (count != 0)
{
cout << "Found\n";
}
else
{
cout << "Not found\n";
}*/
getch();
}
it seems fine. But I think we need to loop in lines 46-50 to print all IP of a hostname
Can you see it and try . If you have any ideas please share me
Thank
Getting onto a Windows box is a real pain for me these days. Anyway, I've run your code as is and it works, but it's not pretty. The output is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cplusplus.com
50.22.100.250
c++.com
Unable to resolve host.
C#.com
Unable to resolve host.
google.com
173.194.41.132
c+++.com
Unable to resolve host.
Your getip() function:
1. calls WSAStartup() twice
2. shouldn't compile as not all paths return a value.
3. uses global variables.
4. calls WSACleanup(), which would interfere in a large program that used sockets.
Do you think the same is true ?
Moreover, to be able to print out IP from a hostname , example google.com have six IP, we need to loop in lines 32-42 . Do you think so ?
With regard to your post here: http://www.cplusplus.com/forum/windows/74966/#msg402760
1. WSAStartup is called in lines 29 and 35.
2. If gethostbyname() fails, or WSAStartup on line 29 fails you have undefined exits. I think the code shouldn't compile, but it does and returns an empty string.
3. You should make your variables as local as possible. What happens if something else uses your global variables in ways you don't expect now as your program gets larger? A real mess the answer.
I posted code for you to demonstrate, the single call to WSAStartup at the start of the program, use of local variables and error handling and how to use the core function you were having problems with, gethostbyname().
You asked about returning a list of ip addresses. I've modified my sample to return that list. I hope this helps.