-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
57 lines (49 loc) · 1.2 KB
/
Copy pathmain.cpp
File metadata and controls
57 lines (49 loc) · 1.2 KB
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
#include "InitSock.h"
#include <iostream>
#include <httplib.h>
using namespace std;
void GetAllIpAddress()
{
char cHost[256] = { 0 };
::gethostname(cHost, 256); //取得本地主机名
hostent* pHost = ::gethostbyname(cHost); //通过主机名得到地址信息
//打印出所有Ip地址
in_addr addr;
for (int i = 0;; i++)
{
char*p = pHost->h_addr_list[i];
if (p == NULL)
{
break;
}
memcpy(&addr.S_un.S_addr, p, pHost->h_length);
char*cIp = ::inet_ntoa(addr);
cout << "IP:" << cIp << endl;
}
}
void GetIpAddress()
{
char cHost[256] = { 0 };
::gethostname(cHost, 256); //取得本地主机名
hostent* pHost = ::gethostbyname(cHost); //通过主机名得到地址信息
//打印本机Ip地址
in_addr addr;
char*p = pHost->h_addr_list[0];
if (p == NULL)
{
return;
}
memcpy(&addr.S_un.S_addr, p, pHost->h_length);
char*cIp = ::inet_ntoa(addr);
cout << "IP:" << cIp << endl;
}
int main()
{
//初始化Winsock库
CInitSock initSock;
//打印所有Ip地址
GetAllIpAddress();
//打印本机Ip地址
//GetIpAddress();
return 0;
}