-
Notifications
You must be signed in to change notification settings - Fork 16
/
getallhostname.cpp
67 lines (54 loc) · 1.61 KB
/
getallhostname.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "getallhostname.h"
GetAllHostName::GetAllHostName()
{
}
GetAllHostName::GetAllHostName(QVector<QString> allIp)
{
// 注册自定义结构参数
qRegisterMetaType< QPair<QString,QString> >("QPair<QString,QString>");
allHostIp = allIp;
}
// 通过局域网内ip地址获取对应的主机名(通过winsock2)
QString GetAllHostName::getHostnameByIpStr(QString ipStr)
{
struct hostent *host;
struct in_addr addr;
QByteArray bytearray = ipStr.toUtf8();
char * ip = bytearray.data();
addr.S_un.S_addr = inet_addr(ip);
host = gethostbyaddr((char *)&addr,sizeof(addr),AF_INET);
if(host != NULL){
//printf("%s\n",host->h_name);
return QString(host->h_name);
}
return "------";
}
void GetAllHostName::run()
{
struct hostent *host;
struct in_addr addr;
QByteArray bytearray;
char * ip;
QString hostname;
QVector<QString>::const_iterator iter = allHostIp.begin();
while(iter != allHostIp.end()){
// 采用局部变量方式
bytearray = (*iter).toUtf8();
ip = bytearray.data();
addr.S_un.S_addr = inet_addr(ip);
host = gethostbyaddr((char *)&addr,sizeof(addr),AF_INET);
if(host != NULL){
//printf("%s\n",host->h_name);
hostname = QString(host->h_name);
}
else hostname = "------";
// 采用调用函数方式
//QString hostname = getHostnameByIpStr(*iter);
QPair<QString,QString> info;
info.first = *iter;
info.second = hostname;
emit getHostnameByIpStrUpdateSig(info);
++iter;
}
this->quit();
}