By requirement, I'm targeting old legacy Windows systems (9x branch) where getaddrinfo()
and freeaddrinfo()
are not available.
What can I use instead of that? The code I'm using right now is extracted from the MSDN site (I'm testing it in a Vista computer):
...
/* WinSock data: */
WSADATA wsaData;
/* Initialize the WinSock data: */
short int iResult = 0;
if (iResult = WSAStartup(MAKEWORD(2,0), &wsaData)) {
printf("Failed to init Winsock library: %d.\n", iResult);
return -1;
}
printf("\n Opening connection to server.");
/* Variables for a connection: */
struct addrinfo *result = NULL, *ptr = NULL, hints;
/* Initialize the connection: */
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
/* Resolve the server address and port: */
if (iResult = getaddrinfo("x", "80", &hints, &am开发者_如何学JAVAp;result)) {
printf("Server resolution failed: %d.\n", iResult);
WSACleanup();
return -2;
}
...
The old way was gethostbyname
and getnameinfo
. Usage is explained well here:
http://beej.us/guide/bgnet/output/html/multipage/gethostbynameman.html
精彩评论