小编Foo*_*kie的帖子

GetAdaptersAddresses() 未给出正确的 IP 地址

我正在使用 USB Internet 加密狗为我的计算机分配 IPv6 地址。当我使用 时ipconfig,我可以看到分配的 IPv6 地址,它是公共 IP。

当我使用 时GetAdaptersAddresses(),我得到了一个长链表,其中包含任播、多播和单播地址,但它们都不与系统的 IP 地址匹配。

还有其他地方可以获取系统的IP吗?我实际上想获取系统的 IP 地址,以便将其绑定到套接字。

我使用Windows 7。

// getadaptersinfo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<WinSock2.h>
#include<iphlpapi.h>
#include<ws2tcpip.h>
#include<iostream>
#pragma comment(lib,"IPHLPAPI.lib")
#pragma comment(lib,"Ws2_32.lib")
using namespace std;

int main()
{
    cout << "\nusing getadapteraddress";
    PIP_ADAPTER_ADDRESSES p, tp;
    ULONG u;
    DWORD ret;
    p = (IP_ADAPTER_ADDRESSES*)HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_ADDRESSES));
    u = sizeof(p);
    GetAdaptersAddresses(0, GAA_FLAG_INCLUDE_PREFIX, NULL, p, &u);
    p = (IP_ADAPTER_ADDRESSES*)HeapAlloc(GetProcessHeap(), 0, u); …
Run Code Online (Sandbox Code Playgroud)

c c++ sockets network-programming visual-c++

1
推荐指数
1
解决办法
5994
查看次数

标签 统计

c ×1

c++ ×1

network-programming ×1

sockets ×1

visual-c++ ×1