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;
    }

  • 相关阅读:
    什么是web框架
    编写CGI程序步骤
    web开发 c/s结构 和 b/s结构
    python自带的web服务器
    自制计算器
    条件判断
    模版继承
    参数传递
    异常处理
    【uoj#244】[UER #7]短路 CDQ分治+斜率优化dp
  • 原文地址:https://www.cnblogs.com/UnGeek/p/2994517.html
Copyright © 2011-2022 走看看