我有一个包含IP地址的变量。我试图做一个NSLOOKUP而不是返回的DNS名称我得到0.我在一个Linux环境。目标IP来自一个向量(字符串dest_ip =向量[2])。gethostbyaddr返回0
#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;
}
再次,而不是接收的主机名,我得到0
为什么不显示你的真实代码?你的例子甚至不会编译。 – 2012-03-22 15:14:53
什么是在dest_ip? – 2012-03-22 15:15:22
“desp_ip”在哪里设置? – 2012-03-22 15:16:12