• 获取本地计算机名和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
  • 相关阅读:
    EcShop二次开发学习方法
    [ 产品经理 ] 互联网产品经理常用软件及工作平台
    Tengine – Nginx衍生版
    把PHP大牛记下来,方便以后关注
    看了极光推送技术原理的几点思考
    centos磁盘满了,查找大文件并清理
    LNMP一键安装包 PHP自动升级脚本
    微信红包系统设计 & 优化
    程序员每天每周每月每年该做的事
    php中$_REQUEST、$_POST、$_GET的区别和联系小结
  • 原文地址:https://www.cnblogs.com/liaocheng/p/4243473.html
Copyright © 2020-2023  润新知