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
|
#undef UNICODE
#include "stdafx.h"
#include <WS2tcpip.h>
#include <WinSock2.h>
#include <string>
#pragma comment(lib,"Ws2_32.lib")
using namespace std;
int _tmain(int argc, char* argv[])
{
WSADATA wsa;
int error = WSAStartup(MAKEWORD(2,2),&wsa);
if (error != 0)
{
printf("An error in startup %d\n",WSAGetLastError());
system("pause");
}
addrinfo hints,
* result = NULL,
* ptr = NULL;
hints.ai_family = AF_UNSPEC;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(argv[1],NULL,&hints,&result);
if (error != 0)
{
printf("An error in getaddrinfo %d\n",WSAGetLastError());
system("pause");
}
char stringbuffer[2075];
int len = sizeof(stringbuffer);
for(ptr = result; ptr->ai_next != NULL; ptr = ptr->ai_next)
{
if(ptr->ai_family == AF_INET)
{
printf("Address: %s\n",InetNtop(ptr->ai_family,ptr->ai_addr,stringbuffer,len));
}
}
return 0;
}
| |