zoukankan      html  css  js  c++  java
  • Windows Socket Programming 网络编程系列 信息获取

    Structure Introduction

    typedef struct hostent {
    char FAR *h_name;
    char FAR FAR **h_aliases;
    short h_addrtype;
    short h_length;
    char FAR FAR **h_addr_list;
    } HOSTENT, *PHOSTENT, FAR *LPHOSTENT;
    struct sockaddr { 
    unsigned short sa_family;
    char sa_data[14];
    };
    struct sockaddr_in{ 
    short sin_family;
    unsigned short sin_port;
    struct in_addr sin_addr;
    char sin_zero[8];
    };

    typedef struct addrinfo {
    int ai_flags;
    int ai_family;
    int ai_socktype;
    int ai_protocol;
    size_t ai_addrlen;
    char *ai_canonname;
    struct sockaddr *ai_addr;
    struct addrinfo *ai_next;
    } ADDRINFOA, *PADDRINFOA;
    typedef struct in_addr {
    union {
    struct {
    u_char s_b1,s_b2,s_b3,s_b4;
    } S_un_b;
    struct {
    u_short s_w1,s_w2;
    } S_un_w;
    u_long S_addr;
    } S_un;
    } IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;

    Retrieving Functions

    For getting struct hostent, using gethostbyaddr or gethostbyname. However both gethostbyname and gethostbyaddr are deprecated on Windows Socket 2. we suggest to use getnameinfo or getaddrinfo to retrieve name of the host or ip info. Here is a sample for using gethostbyaddr and getshotbyname.
    char * tgrIP = "192.168.1.107";//"192.168.1.107";
    struct hostent* rmtHost = nullptr;
    if (!isalpha(tgrIP[0]))
    {
    t1 = time(nullptr);
    unsigned int naddr = inet_addr(tgrIP);
    rmtHost = gethostbyaddr((char *)&naddr, 4, AF_INET);
    if (rmtHost == nullptr)
    {
    int errcode = ::WSAGetLastError();
    cerr<<errcode<<endl;
    return -1;
    }
    cout<<"Using "<<time(nullptr) - t1<<" seconds"<<endl;
    cout<<"Official name:"<<rmtHost->h_name<<endl;
    char ** pAlias;
    for (pAlias = rmtHost->h_aliases; *pAlias != nullptr; pAlias++)
    cout<<"length:"<<rmtHost->h_length<<endl;
    cout<<"aliases:"<<rmtHost->h_aliases<<endl;
    cout<<"addrtype:"<<rmtHost->h_addrtype<<endl;
    if (rmtHost->h_addrtype == AF_INET)
    {
    int i = 0;
    while (rmtHost->h_addr_list[i] != nullptr)
    {
    struct in_addr addr;
    addr.S_un.S_addr = *(u_long *)rmtHost->h_addr_list[i];
    cout<<"IP:"<<inet_ntoa(addr)<<endl;
    i++;
    }
    }
    }
    char * tgrName = www.baidu.com;//"192.168.1.107";
    rmtHost = nullptr;
    if (!isalpha(tgrIP[0]))
    {
    t1 = time(nullptr);

    rmtHost = gethostbyname(tgrName);
    if (rmtHost == nullptr)
    {
    int errcode = ::WSAGetLastError();
    cerr<<errcode<<endl;
    return -1;
    }
    cout<<"Using "<<time(nullptr) - t1<<" seconds"<<endl;
    cout<<"Official name:"<<rmtHost->h_name<<endl;
    char ** pAlias;
    for (pAlias = rmtHost->h_aliases; *pAlias != nullptr; pAlias++)
    cout<<"length:"<<rmtHost->h_length<<endl;
    cout<<"aliases:"<<rmtHost->h_aliases<<endl;
    cout<<"addrtype:"<<rmtHost->h_addrtype<<endl;
    if (rmtHost->h_addrtype == AF_INET)
    {
    int i = 0;
    while (rmtHost->h_addr_list[i] != nullptr)
    {
    struct in_addr addr;
    addr.S_un.S_addr = *(u_long *)rmtHost->h_addr_list[i];
    cout<<"IP:"<<inet_ntoa(addr)<<endl;
    i++;
    }
    }
    }
    For gethostbyaddr, it only supports AF_INET, AF_INET6 and AF_NETBIOS. For gethostbyname, it only returns IPv4 decimal-dot format.
    MSDN recommands using getnameinfo to instead of gethostbyaddr. Here is using getnameinfo sample.
    char hostname[NI_MAXHOST] = {0};
    char servInfo[NI_MAXSERV] = {0};
    unsigned short port = 3389;
    struct sockaddr_in sa;
    ZeroMemory(&sa, sizeof(sockaddr_in));
    sa.sin_family = AF_INET;
    sa.sin_addr.S_un.S_addr = inet_addr(tgrIP);
    sa.sin_port = ::htons(port);
    t1 = time(nullptr);
    int iret = ::getnameinfo((sockaddr*)&sa, sizeof(sockaddr),
    hostname, NI_MAXHOST,
    servInfo, NI_MAXSERV,
    NI_NOFQDN);
    if (iret != 0)
    {
    iret = ::WSAGetLastError();
    cerr<<"Failed to GetAddrInfo: "<<gai_strerror(iret)<<" (ErrCode: "<<iret<<")."<<endl;
    return -1;
    }
    cout<<"Using "<<time(nullptr) - t1<<" seconds"<<endl;
    cout<<hostname<<endl;
    cout<<servInfo<<endl;
    MSDN recommands using getaddrinfo to instead of gethostbyname. Here is using getaddrinfo sample.
    int hname2ip(const char * pname)
    {
    struct addrinfo hints, * result = nullptr;
    memset(&hints, 0, sizeof(struct addrinfo));

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_CANONNAME;

    int ret = getaddrinfo(pname, nullptr, &hints, &result);
    if (ret != 0)
    {
    cerr<<"Error:"<<gai_strerror(ret)<<endl;
    return 1;
    }

    cout<<"Host Name: "<<pname<<endl;
    struct addrinfo * curInfo = nullptr;
    for (curInfo = result; curInfo != nullptr; curInfo = curInfo->ai_next)
    {
    cout<<"IP: "<<inet_ntoa(((struct sockaddr_in *)(curInfo->ai_addr))->sin_addr)<<endl;
    }

    return 0;
    }



     
     


  • 相关阅读:
    tensorflow2.0 GPU和CPU 时间对比
    第一次使用FileZilla Server
    PremiumSoft Navicat 15 for Oracle中文破解版安装教程
    Unmapped Spring configuration files found. Please configure Spring facet or use 'Create Default Context' to add one including all unmapped files.
    ng : 无法加载文件 D: odejs ode_global g.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
    angular
    Github上优秀的go项目
    win10---file explore 中remove quick access folder
    react--useEffect使用
    linux---cat 和 grep 的妙用
  • 原文地址:https://www.cnblogs.com/rogerroddick/p/2918112.html
Copyright © 2011-2022 走看看