`

[C++][Socket] 类似windows Ipconfig的小工具

 
阅读更多

    发现《Windows网络编程技术》中还是有不少很不错的东西哦,比如说类似Ipconfig的demo。贴出来学习一下。

 

#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <time.h>

void main(void) {

    DWORD Err;

    PFIXED_INFO pFixedInfo;
    DWORD FixedInfoSize = 0;

    PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
    DWORD AdapterInfoSize;
    PIP_ADDR_STRING pAddrStr;

    //
    // Get the main IP configuration information for this machine using a FIXED_INFO structure
    //
    if ((Err = GetNetworkParams(NULL, &FixedInfoSize)) != 0)
    {
        if (Err != ERROR_BUFFER_OVERFLOW)
        {
            printf("GetNetworkParams sizing failed with error %d\n", Err);
            return;
        }
    }

    // Allocate memory from sizing information
    if ((pFixedInfo = (PFIXED_INFO) GlobalAlloc(GPTR, FixedInfoSize)) == NULL)
    {
        printf("Memory allocation error\n");
        return;
    }

    if ((Err = GetNetworkParams(pFixedInfo, &FixedInfoSize)) == 0)
    {
        printf("\tHost Name . . . . . . . . . : %s\n", pFixedInfo->HostName);
        printf("\tDNS Servers . . . . . . . . : %s\n", pFixedInfo->DnsServerList.IpAddress.String);
        pAddrStr = pFixedInfo->DnsServerList.Next;
        while(pAddrStr)
        {
            printf("%52s\n", pAddrStr->IpAddress.String);
            pAddrStr = pAddrStr->Next;
        }

        printf("\tNode Type . . . . . . . . . : ");
        switch (pFixedInfo->NodeType)
        {
            case 1:
                printf("%s\n", "Broadcast");
                break;
            case 2:
                printf("%s\n", "Peer to peer");
                break;
            case 4:
                printf("%s\n", "Mixed");
                break;
            case 8:
                printf("%s\n", "Hybrid");
                break;
            default:
                printf("\n");
        }

        printf("\tNetBIOS Scope ID. . . . . . : %s\n", pFixedInfo->ScopeId);
        printf("\tIP Routing Enabled. . . . . : %s\n", (pFixedInfo->EnableRouting ? "yes" : "no"));
        printf("\tWINS Proxy Enabled. . . . . : %s\n", (pFixedInfo->EnableProxy ? "yes" : "no"));
        printf("\tNetBIOS Resolution Uses DNS : %s\n", (pFixedInfo->EnableDns ? "yes" : "no"));
    } else
    {
        printf("GetNetworkParams failed with error %d\n", Err);
        return;
    }

    //
    // Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure.
    // Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.
    //
    AdapterInfoSize = 0;
    if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
    {
        if (Err != ERROR_BUFFER_OVERFLOW)
        {
            printf("GetAdaptersInfo sizing failed with error %d\n", Err);
            return;
        }
    }

    // Allocate memory from sizing information
    if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
    {
        printf("Memory allocation error\n");
        return;
    }

    // Get actual adapter information
    if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
    {
        printf("GetAdaptersInfo failed with error %d\n", Err);
        return;
    }

    pAdapt = pAdapterInfo;

    while (pAdapt)
    {
        switch (pAdapt->Type)
        {
            case MIB_IF_TYPE_ETHERNET:
                printf("\nEthernet adapter ");
                break;
            case MIB_IF_TYPE_TOKENRING:
                printf("\nToken Ring adapter ");
                break;
            case MIB_IF_TYPE_FDDI:
                printf("\nFDDI adapter ");
                break;
            case MIB_IF_TYPE_PPP:
                printf("\nPPP adapter ");
                break;
            case MIB_IF_TYPE_LOOPBACK:
                printf("\nLoopback adapter ");
                break;
            case MIB_IF_TYPE_SLIP:
                printf("\nSlip adapter ");
                break;
            case MIB_IF_TYPE_OTHER:
            default:
                printf("\nOther adapter ");
        }
        printf("%s:\n\n", pAdapt->AdapterName);

        printf("\tDescription . . . . . . . . : %s\n", pAdapt->Description); 

        printf("\tPhysical Address. . . . . . : ");
        for (UINT i=0; i<pAdapt->AddressLength; i++)
        {
            if (i == (pAdapt->AddressLength - 1))
                printf("%.2X\n",(int)pAdapt->Address[i]);
            else
                printf("%.2X-",(int)pAdapt->Address[i]);
        }        

        printf("\tDHCP Enabled. . . . . . . . : %s\n", (pAdapt->DhcpEnabled ? "yes" : "no"));

        pAddrStr = &(pAdapt->IpAddressList);
        while(pAddrStr)
        {
            printf("\tIP Address. . . . . . . . . : %s\n", pAddrStr->IpAddress.String);
            printf("\tSubnet Mask . . . . . . . . : %s\n", pAddrStr->IpMask.String);
            pAddrStr = pAddrStr->Next;
        }

        printf("\tDefault Gateway . . . . . . : %s\n", pAdapt->GatewayList.IpAddress.String);
        pAddrStr = pAdapt->GatewayList.Next;
        while(pAddrStr)
        {
            printf("%52s\n", pAddrStr->IpAddress.String);
            pAddrStr = pAddrStr->Next;
        }

        printf("\tDHCP Server . . . . . . . . : %s\n", pAdapt->DhcpServer.IpAddress.String);
        printf("\tPrimary WINS Server . . . . : %s\n", pAdapt->PrimaryWinsServer.IpAddress.String);
        printf("\tSecondary WINS Server . . . : %s\n", pAdapt->SecondaryWinsServer.IpAddress.String);

        struct tm *newtime;

        // Display coordinated universal time - GMT 
        newtime = gmtime(&pAdapt->LeaseObtained);   
        printf( "\tLease Obtained. . . . . . . : %s", asctime( newtime ) );

        newtime = gmtime(&pAdapt->LeaseExpires);   
        printf( "\tLease Expires . . . . . . . : %s", asctime( newtime ) );

        pAdapt = pAdapt->Next;
    }
}
 
分享到:
评论

相关推荐

    VC实现ipconfig指令源码

    - 实现`ipconfig`功能,我们需要了解Winsock(Windows Socket)API,它是Windows下的网络编程接口。 3. Winsock API: - 使用Winsock API可以访问底层网络协议栈,获取网络接口的信息。 - 关键函数包括`...

    C++套接字编程,工具VS2019

    在Windows系统中,可以使用命令行工具`ipconfig/all`来查看本机的IP配置信息。找到“IPv4 地址”或“IPv6 地址”,这些就是可以用于网络通信的IP地址。在客户端程序中,需要将这些IP地址替换到`client.cpp`中的相应...

    C++_Builder的60个编程资料

    - **特点**:C++ Builder具有直观的界面设计工具、高效的编译器以及强大的调试功能。 #### 2. 文件与目录操作 - **知识点**:C++ Builder提供了一系列API来处理文件和目录的操作,包括文件的创建、删除、复制,以及...

    socket网络编程原理说明

    在C++中,你可以根据需求选择不同的软件框架来实现socket通信。例如,你可以创建控制台应用程序,它只包含一个黑窗口,没有预设的用户界面。另一种选择是Windows应用程序,它允许你利用Windows API创建带有图形界面...

    C++实现获取IP程序

    以下是一个简单的C++程序,它使用系统调用执行`ifconfig`(在Unix-like系统)或`ipconfig`(在Windows系统)命令,并解析输出以获取IP地址: ```cpp #include #include #include #include #include std::...

    熟悉网络命令socket编程.doc

    由于实验要求不使用MFC的Csocket类库和C++ Builder,因此需要直接操作Win32 Socket API,这将涉及更多的网络协议细节和错误处理。 实验的挑战在于设计一个自定义的FTP协议,包括应用层PDU格式和握手方式。对于有更...

    RegEditMFCDemo_获取当前网络信息_通过注册表修改当前IP_

    在Windows中,可以通过多种方式获取网络连接的状态和配置信息,包括使用命令行工具(如`ipconfig`)和编程接口(如Windows Socket API, WMI - Windows Management Instrumentation)。`ipconfig`命令是最直接的方法...

    获取电脑硬件的详细信息,与注册有关,物理地址,序列号

    获取IP地址的方法多种多样,如通过命令行工具(Windows下的`ipconfig`,Linux或MacOS下的`ifconfig`)或者网络编程接口(如Python的socket库)。 2. 硬盘ID:硬盘ID通常指的是硬盘的序列号,它是每个硬盘出厂时被...

    取得本地IP地址与机器名

    - 在Python中,可以使用`socket.gethostbyname(socket.gethostname())`来获取IP地址,`socket.gethostname()`获取机器名。 - C/C++开发者可以使用`gethostname()`和`getaddrinfo()`函数来获取这些信息。 总的来说...

    获取设备参数

    - C++:利用POSIX或Windows API,如`sys/sysinfo.h`(Linux)或`GetSystemInfo`(Windows)。 5. **网络设备参数**: - IP地址和子网掩码:通过`ifconfig`(Linux、Mac)或`ipconfig`(Windows)命令。 - 网络...

    获取计算机名称和IP

    在Windows操作系统中,你可以通过以下方法获取计算机名称: 1. 打开“控制面板” -&gt; “系统和安全” -&gt; “系统”,在弹出的窗口中可以看到“计算机名/域/工作组”下的计算机名。 2. 使用命令行工具“cmd”,输入`...

    本机IP地址查询程序

    - Windows命令行:使用`ipconfig`命令可以查看本机所有网络接口的IP地址,包括IPv4和IPv6。 - Unix/Linux终端:使用`ifconfig`(较旧系统)或`ip addr show`(较新系统)命令来查看IP信息。 - 通过编程语言:例如...

    获得本机的IP地址!

    - **Python**:可以使用`socket`库中的`gethostbyname()`或`getaddrinfo()`函数来获取IP地址。 - **Java**:通过`java.net.NetworkInterface`和`java.net.InetAddress`类,遍历所有网络接口并获取IP。 - **C#**:...

    win764过tp双机调试

    可以使用命令行工具如`ipconfig`(Windows)或`ifconfig`(Linux/Mac)获取本地IP地址,端口号一般自定义,但要确保不与其他服务冲突,常用的是1024-49151之间的非保留端口。 5. **调试步骤**: - **创建服务器端*...

    获取本机网卡信息(包括ipv6 地址信息)

    在Windows系统中,可以使用命令行工具如`ipconfig /all`来查看所有网卡的详细信息,包括MAC地址(物理地址)和IPv6地址。在Linux系统中,可以使用`ifconfig`或`ip addr show`命令来获取类似信息。在Unix-like系统中...

    计算机网络安全试验指南

    - 选择合适的操作系统(如Windows 2000 Advanced Server)。 - 配置虚拟机参数,包括内存大小、处理器数量等。 2. **配置虚拟机**: - 使用“File”菜单中的“Open”选项加载操作系统镜像文件。 - 启动两个...

    获取主机ip以及修改xml文件

    在Windows系统中,可以通过命令行工具“cmd”来获取IP地址: 1. 打开命令提示符(按Win+R键,输入cmd并回车)。 2. 输入命令`ipconfig`,回车。这将列出所有网络接口的IP信息,包括IPv4和IPv6地址。 3. 查找与你的...

    获取系统信息代码1

    3. **网络信息**:获取网络状态,如IP地址、MAC地址、网络接口状态等,可以使用`socket`库(Python)、`ifconfig`命令(Linux)或`ipconfig`命令(Windows)。Python中还可以利用`psutil`库的网络接口来获取网络信息...

Global site tag (gtag.js) - Google Analytics