· 8 years ago · Dec 24, 2017, 05:14 PM
1char* ip = argv[1];
2// using gethostbyaddr()
3hostent * phe = gethostbyaddr(ip, strlen(ip), AF_INET);
4if(phe) {
5 cout << phe->h_name << "n";
6}
7
8// using getnameinfo()
9char hostname[260];
10char service[260];
11sockaddr_in address;
12memset(&address, 0, sizeof(address));
13address.sin_family = AF_INET;
14address.sin_addr.s_addr = inet_addr(ip);
15int response = getnameinfo((sockaddr*)&address,
16 sizeof(address),
17 hostname,
18 260,
19 service,
20 260,
21 0);
22if(response == 0) {
23 cout << hostname << "n";
24}
25
26#include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29
30 #include <winsock2.h>
31
32 int main(){
33 struct addrinfo hints;
34 struct addrinfo *res=0;
35 int status;
36
37 WSADATA wsadata;
38 int statuswsadata;
39 if((statuswsadata=WSAStartup(MAKEWORD(2,2),&wsadata))!=0){
40 printf("WSAStartup failed: %dn",statuswsadata);
41 }
42
43 hints.ai_family =AF_INET;
44
45 status=getaddrinfo("87.250.251.11",0,0,&res);
46
47 {
48 char host[512],port[128];
49
50 status=getnameinfo(res->ai_addr,res->ai_addrlen,host,512,0,0,0);
51
52 printf("Host: %s",host);
53
54 freeaddrinfo(res);
55 }
56 }
57
58d:tempstack>ip
59Host: yandex.ru
60
61C:Usersuser>ping yandex.ru
62
63Pinging yandex.ru [87.250.251.11] with 32 bytes of data:
64Reply from 87.250.251.11: bytes=32 time=21ms TTL=56
65Reply from 87.250.251.11: bytes=32 time=21ms TTL=56
66Reply from 87.250.251.11: bytes=32 time=21ms TTL=56
67
68Ping statistics for 87.250.251.11:
69 Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
70Approximate round trip times in milli-seconds:
71 Minimum = 21ms, Maximum = 21ms, Average = 21ms