当前位置:编程学习 > C/C++ >>

C++获取本机IP地址

std::string GetLocalIpAddress()   
{   
    WORD wVersionRequested = MAKEWORD(2, 2);   
    WSADATA wsaData;   
    if (WSAStartup(wVersionRequested, &wsaData) != 0)   
        return "";   
    char local[255] = {0};   
    gethostname(local, sizeof(local));   
    hostent* ph = gethostbyname(local);   
    if (ph == NULL)   
        return "";   
    in_addr addr;   
    memcpy(&addr, ph->h_addr_list[0], sizeof(in_addr));   
    std::string localIP;   
    localIP.assign(inet_ntoa(addr));   
    WSACleanup();   
    return localIP;   
}   
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,