zoukankan      html  css  js  c++  java
  • gethostbyname

    #include "stdafx.h"
    #include <stl_h.hpp>
    #include <CInitSocket.hpp>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #pragma comment(lib,"ws2_32.lib")
    CInitSocket init;
    using namespace std;
    int main(int argc, char **argv)
    {
        char *ptr,**pptr;
        struct hostent *hptr;
        char str[32];
        /* 取得命令后第一个参数,即要解析的域名或主机名 */
        ptr = "shell.cjb.net";
        /* 调用gethostbyname()。调用结果都存在hptr中 */
        if( (hptr = gethostbyname(ptr) ) == NULL )
        {
            printf("gethostbyname error for host:%s\n", ptr);
            return 0; /* 如果调用gethostbyname发生错误,返回1 */
        }
        /* 将主机的规范名打出来 */
        printf("official hostname:%s\n",hptr->h_name);
        /* 主机可能有多个别名,将所有别名分别打出来 */
        for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
            printf(" alias:%s\n",*pptr);
        /* 根据地址类型,将地址打出来 */
        switch(hptr->h_addrtype)
        {
        case AF_INET:
        case AF_INET6:
            pptr=hptr->h_addr_list;
            /* 将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 */
            for(;*pptr!=NULL;pptr++)
                printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
            break;
        default:
            printf("unknown address type\n");
            break;
        }
        return 0;
    }

  • 相关阅读:
    英语常用口语
    单词记忆(3)
    电视制式及声音制式的划分
    单词记忆(2)
    单词记忆(1)
    低调做人 高调做事
    分析数据库死锁原因的方法
    详细查看数据库SQL执行计划
    如果查看数据库链接数
    刷新所有视图
  • 原文地址:https://www.cnblogs.com/UnGeek/p/2994517.html
Copyright © 2011-2022 走看看