获取主机IP地址

    LPHOSTENT hostEntry = NULL;
    char hostName[BUFSIZE];
    gethostname(hostName, BUFSIZE);
    hostEntry = gethostbyname(hostName);
    if (!hostEntry)
    {
        AfxMessageBox(_T("获取主机IP地址失败"));
        return FALSE;
    }

    // 设置sockaddr_in
    SOCKADDR_IN addrServ;
    addrServ.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    addrServ.sin_family = AF_INET;
    addrServ.sin_port = htons(SERVERPORT);

    char * chIP = inet_ntoa(addrServ.sin_addr);
    AfxMessageBox(chIP);

 

posted @ 2013-08-21 10:10  BornThisWay  阅读(269)  评论(0编辑  收藏  举报