This drag me mad
#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;
}
getaddrinfo throw 11003 error, i've compared a lot of internet source with main but i can't figure out why getaddrinfo fail! i've read the winsock error code description and it says that 11003 "indicates that some sort of nonrecoverable error occurred during a database lookup" so thanks in advance ! EDIT: i've asked somewhere else but none give me the solution,i'm blocked on this and i can't move forward
As nos has mentioned in comments
memset(&hints, 0, sizeof(hints))
should do the trick.
getaddrinfo()
tries to fill up with existing data provided. Garbage data is confusing the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With