获得主机名 和 IP地址

来源:互联网 发布:外汇tick数据下载 编辑:程序博客网 时间:2024/05/07 12:52

1.获得主机名

//获得主机名
    gethostname(szHostName, 128);
    CString str;

    for(int i = 0; i <= 128; i++)
    {
        if(szHostName[i] != '/0')
            str.Insert(i,szHostName[i]);

        if(szHostName[i] == '/0')
        {
            MessageBox(_T("OUT"));
            MessageBox(str);
            break;
        }
    }

 

 

2.获得IP地址

struct hostent* pHost;

//获得IP地址
        pHost = gethostbyname(szHostName);
        for(i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++)
        {
            char str[100];
            char addr[20];
            int j;
            LPCSTR psz = inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]);
            m_IP.AddString(psz);
           
        }                                                                                              by《VC网络编程》

原创粉丝点击