用于命名管道连接的本地IP是什么? [重复]

问题描述 投票:0回答:1

这甚至与带有 IP 地址的 命名管道连接的副本相差甚远

本地系统上的 Windows C++ 应用程序,具有多个适配器和 IP 地址,通过名称使用命名管道连接到远程系统。如何找出建立连接时使用的 IP 地址?

为了更多地理解这个问题:

  • 我知道如何获取所有本地系统适配器和 IP 地址。
  • 我知道如何创建并连接到两端的命名管道。

我需要知道的是:

  • 您如何知道哪个适配器(即 IP 或网络)用于命名管道连接?

这里涉及到多个网络和适配器。

winapi networking named-pipes
1个回答
-2
投票
  • 您可以调用
    GetAdaptersInfo
    函数获取网卡信息。
  • 如果想获取所有网卡的IP和MAC地址信息 在机器上,您必须按顺序获取每个网卡,然后获取 它的信息。

在Windows SDK中,可以使用

IP_ADAPTER_INFO
结构来存储网卡信息,包括网卡名称、网卡描述、网卡MAC地址、网卡IP地址等。 这里是IP_ADAPTER_INFO的描述

我还有一个样品。希望有帮助。

#include <WinSock2.h>
#include <Iphlpapi.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"Iphlpapi.lib") 

int main(int argc, char* argv[])
{

    PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
    
    unsigned long stSize = sizeof(IP_ADAPTER_INFO);
    int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
    int netCardNum = 0;
    int IPnumPerNetCard = 0;
    if (ERROR_BUFFER_OVERFLOW == nRel)
    {
        delete pIpAdapterInfo;
        
        pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
       
        nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
    }
    if (ERROR_SUCCESS == nRel)
    {
        while (pIpAdapterInfo)
        {
            cout << "numbers of NICs:" << ++netCardNum << endl;
            cout << "Name of NICs:" << pIpAdapterInfo->AdapterName << endl;
            cout << "discription of NICs:" << pIpAdapterInfo->Description << endl;
            switch (pIpAdapterInfo->Type)
            {
            case MIB_IF_TYPE_OTHER:
                cout << "Type of NICs:" << "OTHER" << endl;
                break;
            case MIB_IF_TYPE_ETHERNET:
                cout << "Type of NICs:" << "ETHERNET" << endl;
                break;
            case MIB_IF_TYPE_TOKENRING:
                cout << "Type of NICs:" << "TOKENRING" << endl;
                break;
            case MIB_IF_TYPE_FDDI:
                cout << "Type of NICs:" << "FDDI" << endl;
                break;
            case MIB_IF_TYPE_PPP:
                printf("PP\n");
                cout << "Type of NICs:" << "PPP" << endl;
                break;
            case MIB_IF_TYPE_LOOPBACK:
                cout << "Type of NICs:" << "LOOPBACK" << endl;
                break;
            case MIB_IF_TYPE_SLIP:
                cout << "Type of NICs:" << "SLIP" << endl;
                break;
            default:

                break;
            }
            cout << "NICs MAC  address:";
            for (DWORD i = 0; i < pIpAdapterInfo->AddressLength; i++)
                if (i < pIpAdapterInfo->AddressLength - 1)
                {
                    printf("%02X-", pIpAdapterInfo->Address[i]);
                }
                else
                {
                    printf("%02X\n", pIpAdapterInfo->Address[i]);
                }
            cout << "Here are NICs IPs address:" << endl;
            
            IP_ADDR_STRING* pIpAddrString = &(pIpAdapterInfo->IpAddressList);
            do
            {
                cout << "Number of IP of the NIC:" << ++IPnumPerNetCard << endl;
                cout << "IP address:" << pIpAddrString->IpAddress.String << endl;
                cout << "Subnet address:" << pIpAddrString->IpMask.String << endl;
                cout << "Gateway address:" << pIpAdapterInfo->GatewayList.IpAddress.String << endl;
                pIpAddrString = pIpAddrString->Next;
            } while (pIpAddrString);
            pIpAdapterInfo = pIpAdapterInfo->Next;
            cout << "--------------------------------------------------------------------" << endl;
        }

    }
    if (pIpAdapterInfo)
    {
        delete pIpAdapterInfo;
    }

    return 0;
}

执行结果:

© www.soinside.com 2019 - 2024. All rights reserved.