开发者

Problem in GetHostByName & inet_ntoa in MFC(VC++ )

I am using below code for getting IP from passing domain name. It is returning me proper IP but now when some network setting is changed Server Ip is also changed . Now als0 it is returning me that Old IP not the new one. Any help is highly appreciared.

CString CNDSClientDlg::GetIPFromDomain(char* cDomainName)
{
        if(cDomainName == NULL)
        {
            MessageBox("Invalid Domain Name","Network Drive Solution", MB_ICONERROR | MB_OK);   
            return "";
        }
        char *cIPAddress = NULL;
        WSADATA wsaData = {0};
        int iResult = 0;        
        hostent *remoteHost = NULL;     
        struct开发者_运维技巧 in_addr addr;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) 
    {           
        MessageBox("WSAStartup failed","Network Drive Solution", MB_ICONERROR | MB_OK); 
        return "";

    }

    remoteHost = gethostbyname(cDomainName);
    addr.s_addr = *(u_long *) remoteHost->h_addr_list[0];       
    cIPAddress =  inet_ntoa(addr);

    return cIPAddress;

}


You probably get the address from your DNS cache.

Use ipconfig /flushdns to clear the cache.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜