gpt4 book ai didi

c++ - 使用 WSAIoctl 函数统计网络接口(interface)(WIN32 API)

转载 作者:行者123 更新时间:2023-11-28 08:33:14 27 4
gpt4 key购买 nike

我正在尝试使用 WSAIoctl 函数列出可用的接口(interface)。我必须传入一个缓冲区来保存完整列表。我想在分配内存以保存接口(interface)详细信息之前获取接口(interface)的计数,但是如果我传入 NULL 指针,调用就会失败(我没有得到返回的有效计数)。在我必须分配内存之前有什么方法可以得到这个计数?

背景是我正在 Windows 机器上启动大量进程/线程,它们都连接到单个服务器。我希望服务器将这些单独的连接视为来自不同的 IP 地址,并且我已将大量别名添加到测试机器以允许这样做(很多)。 WSAIoct 确实正确地找到了我添加的所有内容。

干杯...

最佳答案

来自msdn documentation对于 WSAIoctl:

Note: If the output buffer is not large enough to contain the address list, SOCKET_ERROR is returned as the result of this IOCTL and WSAGetLastError returns WSAEFAULT. The required size, in bytes, for the output buffer is returned in the lpcbBytesReturned parameter in this case. Note the WSAEFAULT error code is also returned if the lpvInBuffer, lpvOutBuffer, or lpcbBytesReturned parameter is not completely contained in a valid part of the user address space.

所以你必须调用 WSAIoctl 函数两次。第一次使用任意缓冲区,然后检查文档中提到的错误代码。然后使用 lpcbBytesReturned 中返回的大小来分配缓冲区并再次调用 WSAIoctl 函数。

关于c++ - 使用 WSAIoctl 函数统计网络接口(interface)(WIN32 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/862934/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com