zoukankan      html  css  js  c++  java
  • 获取本地计算机名和IP地址

    WSADATA wsadata;
    if(0 != WSAStartup(MAKEWORD(2, 2), &wsadata))
    {
      AfxMessageBox("初始化网络环境失败!");
      return TRUE;
    }
    char szHostName[MAX_PATH + 1]; gethostname(szHostName, MAX_PATH); hostent *p = gethostbyname(szHostName); if(p == NULL) {   AfxMessageBox("得到本机网络信息失败!");   return TRUE; } m_strName = szHostName; int nCount = 0; char *pIP = inet_ntoa(*(in_addr *)p->h_addr_list[0]); m_strAddr = pIP;
    #include <winsock2.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    #pragma comment(lib,"Iphlpapi.lib")
    int main()
    {
    // 初始化winsock
    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
    DWORD dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
    // 第一次调用GetAdapterInfo获取ulOutBufLen大小
    if (dwRetVal == ERROR_BUFFER_OVERFLOW)
    {
    free(pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
    dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
    }
    if (dwRetVal == NO_ERROR)
    {
    pAdapter = pAdapterInfo;
    while (pAdapter)
    {
    printf("Adapter Name: 	%s
    ", pAdapter->AdapterName);
    printf("Adapter Desc: 	%s
    ", pAdapter->Description);
    printf("MAC Addr: 	%02x-%02x-%02x-%02x-%02x-%02x
    ",
    pAdapter->Address[0],
    pAdapter->Address[1],
    pAdapter->Address[2],
    pAdapter->Address[3],
    pAdapter->Address[4],
    pAdapter->Address[5]);
    printf("IP Address: 	%s
    ", pAdapter->IpAddressList.IpAddress.String);
    printf("IP Mask: 	%s
    ", pAdapter->IpAddressList.IpMask.String);
    printf("Gateway: 	%s
    ", pAdapter->GatewayList.IpAddress.String);
    pAdapter = pAdapter->Next;
    }// end while
    }
    else
    {
    printf("Call to GetAdaptersInfo failed.
    ");
    }
    ...卸载winsock ...
    return 0;
    } //end main
  • 相关阅读:
    机器学习之--画图补充
    机器学习之--KNN算法简单实现
    redhat centos yum源的安装
    redhat6.5 linux 安装mysql5.6.27
    bash 截取字符串
    redhat vim编辑器永久添加行号及搜索
    Orthomcl的详细使用
    InterProScan 5.25-64.0 安装和使用
    paml正选择处理时序列里有终止密码子怎么处理掉
    R语言putty中直接使用X11(Xming)绘图
  • 原文地址:https://www.cnblogs.com/liaocheng/p/4243473.html
Copyright © 2011-2022 走看看