lizhen 发表于 2017-6-2 13:22:48

获取本机IP地址

#include "InitSock.h"
#include <stdio.h>

void main()
{
        CInitSock initSock;
       
        char szHost = {0};
        ::gethostname(szHost, 256);
        hostent* pHost = ::gethostbyname(szHost);
        //in_addr是表示IP地址的结构
        in_addr addr;
        for (int i=0;;i++)
        {
                char* p = pHost->h_addr_list;
                if (p == NULL)
                {
                        break;
                }
                memcpy(&amp;addr.S_un.S_addr, p, pHost->h_length);
                char* szIP = ::inet_ntoa(addr);
                printf("本机IP地址为:%s\n", szIP);
        }
        printf("*******************************");
        getchar();
}
打印结果:
页: [1]
查看完整版本: 获取本机IP地址