根据要求,我的目标是旧的Windows旧系统(9x分支),其中getaddrinfo()freeaddrinfo()不可用。

我可以用什么代替呢?我现在使用的代码是从MSDN站点中提取的(我正在Vista计算机中对其进行测试):

...
/* 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, &result)) {
    printf("Server resolution failed: %d.\n", iResult);
    WSACleanup();
    return -2;
}
...

最佳答案

旧方法是gethostbynamegetnameinfo。这里对用法进行了很好的解释:

http://beej.us/guide/bgnet/output/html/multipage/gethostbynameman.html

关于c - 代替传统Windows的getaddrinfo()和freeaddrinfo()使用什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2517879/

10-11 11:51