针对旧版 Windows,可以使用什么来代替 getaddrinfo() 和 freeaddrinfo()?

发布于 2024-08-26 23:10:47 字数 924 浏览 7 评论 0原文

根据要求,我的目标是旧版 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;
}
...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-09-02 23:10:47

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

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

The old way was gethostbyname and getnameinfo. Usage is explained well here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文