zoukankan      html  css  js  c++  java
  • VC++ 获取本地IP和计算机名

    主要是两个函数的使用,gethostname();、gethostbyname();

    自定义两个函数GetLocalHostName获取计算机名、GetIPAddress获取IP地址

     1 int CIPDlg::GetLocalHostName(CString &strHostName)
     2 {
     3     char szHostName[256];
     4     int nRetCode;
     5     nRetCode = gethostname(szHostName, sizeof(szHostName));
     6     if(nRetCode != 0)
     7     {
     8         strHostName = _T("Not available");
     9         return WSAGetLastError();
    10     }
    11     strHostName = szHostName;
    12     return 0;
    13 }
     1 int CIPDlg::GetIPAddress(const CString &strHostName, CString &strIPAddress)
     2 {
     3     struct hostent FAR *lpHostEnt = gethostbyname(strHostName);
     4     if(lpHostEnt == NULL)
     5     {
     6         strIPAddress = _T("");
     7         return WSAGetLastError();
     8     }
     9     LPSTR lpAddr = lpHostEnt->h_addr_list[0];
    10     if(lpAddr)
    11     {
    12         struct in_addr inAddr;
    13         memmove(&inAddr, lpAddr, 4);
    14         strIPAddress = inet_ntoa(inAddr);
    15         if(strIPAddress.IsEmpty())
    16         {
    17             strIPAddress = _T("Not avilable");
    18         }
    19     }
    20     return 0;
    21 }
  • 相关阅读:
    求解答可用性测试记
    Teambition可用性测试记
    海丁网可用性测试记
    go语言的切片
    go语言的数组
    go语言的函数
    go语言的接口
    go语言的结构体
    go语言的flag
    创建二叉树和三种遍历
  • 原文地址:https://www.cnblogs.com/lit10050528/p/3634356.html
Copyright © 2011-2022 走看看