• 名字与地址转换


    gethostbyname/gethostbyaddr

    均用于IPv4中, 第一个通过域名查ip地址, 第二个通过ip地址反向查询域名.
    不过需要注意的反向查询需要本地有反射解析服务, 或者说本地的etc/hosts文件中有相应的记录

    #include <netdb.h>
    struct hostent *gethostbyname(const char *hostname);
    struct hostent *gethostbyaddr(const char *addr,socklen_t len,int family);
     
     
    struct hostent{
        char *h_name;   //域名
     
        char **h_aliases; //别名
     
        int h_addrtype;  //AF_INET
     
        int h_length; // 4
     
        char **h_addr_list; //域名对应的IPv4地址链
     
    }
    

    gethostbyname的参数是域名的字符串
    gethostbyaddr的第一个参数是IPv4地址的二进制字符串

    #include "unp.h"
    #include <netdb.h>
     
    int main(int argc, char *argv[])
    {
        char *ptr,**pptr;
        char str[INET_ADDRSTRLEN];
        struct hostent *hptr;
     
        while(--argc > 0){
            ptr = *++argv;
            if((hptr=gethostbyname(ptr)) == NULL){
                printf("gethostbyname error for host: %s: %s",
                        ptr,hstrerror(h_errno));
                continue;
            }
            printf("official hostname: %s
    ",hptr->h_name);
     
            for(pptr=hptr->h_aliases;*pptr != NULL;pptr++)
                printf("	alias: %s
    ",*pptr);
            switch(hptr->h_addrtype){
                case AF_INET:
                    pptr=hptr->h_addr_list;
                    for(;*pptr != NULL;pptr++)
                        printf("	address: %s
    ",inet_ntop(hptr->h_addrtype,*pptr,str,sizeof(str)));
                    break;
                default:
                    printf("unknown address type
    ");
                    break;
            }
        }
        return 0;
    }
    
    #include "unp.h"
    #include <netdb.h>
     
    int main(int argc, char *argv[])
    {
        char *ptr,**pptr;
        char str[INET_ADDRSTRLEN];
        struct hostent *hptr;
        struct in_addr *addr;
        struct sockaddr_in saddr;
     
        while(--argc > 0){
            ptr = *++argv;
            if(!inet_aton(ptr,&saddr.sin_addr))
                err_quit("inet_aton error");
     
            if((hptr=gethostbyaddr((void *)&saddr.sin_addr,sizeof(struct in_addr),AF_INET)) == NULL)
                err_quit("gethostbyaddr error:%s %s ",ptr, hstrerror(h_errno) );
     
            printf("official hostname: %s
    ",hptr->h_name);
     
            for(pptr=hptr->h_aliases;*pptr!=NULL;pptr++)
                printf("	alias: %s
    ",*pptr);
     
            switch(hptr->h_addrtype){
                case AF_INET:
                    pptr=hptr->h_addr_list;
                    for(;*pptr != NULL;pptr++)
                        printf("	address:%s
    ",
                                inet_ntop(hptr->h_addrtype,*pptr,str,sizeof(str)));
                    break;
                default:
                    puts("unknown address type
    ");
                    break;
            }
        }
        return 0;
    }
    

    getservbyname/getservbyport

    大致就是输入http输出80; 输入21输出ftp

    #include <netdb.h>
    struct servent *getservbyname(const char *servname, const char *protoname);
    struct servent *getservbyport(int port, const char *protoname);
     
     
    struct servent{
        char *s_name; /* http,ftp... */
        char **s_aliases; /* 别名 */
        int s_port; /* 端口号 */
        char *s_proto; /* tcp 或 udp */
    }
    

    getservbyport中的port参数需要网络字节序

    #include "unp.h"
    #include <netdb.h>
     
    int main(int argc, char *argv[])
    {
        struct servent *ent;
     
        if(argc < 2)
            err_quit("usage: ./port2name [port]");
        if((ent=getservbyport(htons(atoi(argv[1])),"tcp")) == NULL)
            err_quit("getservent error");
     
        printf("official name: %s
    ",ent->s_name);
        printf("port %d
    ",ntohs(ent->s_port));
        return 0;
    }
    
    #include "unp.h"
    #include <netdb.h>
     
    int main(int argc, char *argv[])
    {
        struct servent *ent;
     
        if(argc < 2)
            err_quit("usage: ./name2port [service_name]");
        if((ent=getservbyname(argv[1],"tcp")) == NULL)
            err_quit("getservent error");
     
        printf("official name: %s
    ",ent->s_name);
        printf("port %d
    ",ntohs(ent->s_port));
        return 0;
    }
    
  • 相关阅读:
    好用的Win7下硬盘分区软件:Acronis Disk Director Suite
    SQL Server 相关create操作语句
    我也有博客了
    N层构架如何实现
    SQL相关增删改查语句
    1.MVC的工作流程
    回顾去年以来读过的书
    [Architecture]Facebook Chat
    [Tips]解决make_sock: could not bind to address 0.0.0.0:XXXX
    Emacs中的按键组合
  • 原文地址:https://www.cnblogs.com/cfans1993/p/6131909.html
Copyright © 2020-2023  润新知