• Unix Network Programming Episode 77


    ‘gethostbyaddr’ Function

    The function gethostbyaddr takes a binary IPv4 address and tries to find the hostname corresponding to that address. This is the reverse of gethostbyname.

    #include 
    struct hostent *gethostbyaddr (const char *addr, socklen_t len, int family);
    
    • 1
    • 2
    ‘getservbyname’ and ‘getservbyport’ Functions

    Services, like hosts, are often known by names, too. If we refer to a service by its name in our code, instead of by its port number, and if the mapping from the name to port number is contained in a file (normally /etc/services), then if the port number changes, all we need to modify is one line in the /etc/services file instead of having to recompile the applications. The next function, getservbyname, looks up a service given its name.

    #include 
    struct servent *getservbyname (const char *servname, const char *protoname);
    
    • 1
    • 2

    This function returns a pointer to the following structure:

    struct servent {
    	char *s_name; /* official service name */
    	char **s_aliases; /* alias list */
    	int s-port; /* port number, network-byte order */
    	char *s_proto; /* protocol to use */
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    The service name servname must be specified. If a protocol is also specified (protoname is a non-null pointer), then the entry must also have a matching protocol.

    The next function, getservbyport, looks up a service given its port number and an optional protocol.

    #include 
    struct servent *getservbyport (int port, const char *protoname);
    
    • 1
    • 2
    #include "unp.h"
    
    int main(int argc, char **argv)
    {
        int sockfd, n;
        char recvline[MAXLINE+1];
        struct sockaddr_in servaddr;
        struct in_addr **ptr;
        struct in_addr *inetaddrp[2];
        struct in_addr inetaddr;
        struct hostent *hp;
        struct servent *sp;
    
        if(argc!=3){
            err_quit("usage: daytimetcpclientl  ");
        }
    
        if((hp=gethostbyname(argv[1]))==NULL)
        {
            if(inet_aton(argv[1],&inetaddr)==0)
            {
                err_quit("hostname error for %s: %s",argv[1], hstrerror(h_errno));
            }else{
                inetaddrp[0]=&inetaddr;
                inetaddrp[1]=NULL;
                pptr=inetaddrp;
            }
        }
        else
        {
            pptr=(struct in_addr **)hp->h_addr_list;
        }
    
        if((sp=getservyname(argv[2],"tcp"))==NULL)
        {
            err_quit("getservbyname error for %s", argv[2]);
        }
        for(;*pptr!=NULL;pptr++)
        {
            sockfd=Socket(AF_INET, SOCK_STREAM,0);
    
            bzero(&servaddr, sizeof(servaddr));
            servaddr.sin_family=AF_INET;
            servaddr.sin_port=sp->s_port;
            memcpy(&servaddr.sin_addr, *pptr, sizeof9struct in_addr));
            printf("trying %s\n", Sock_ntop((SA *)&servaddr, sizeof(servaddr)));
    
            if(connect(sockfd, (SA *)&servaddr, iszoef9servaddr))==0)
                break;
            err_ret("connect error");
            close(sockfd);
        }
        if(*pptr==NULL)
        {
            err_quit("unable to connect");
        }
    
        while((n=Read(sockfd, recvline,MAXLINE))>0)
        {
            recvlien[n]=0;
            Fputs(recvline, stdout);
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    Our daytime client that uses gethostbyname and getservbyname

  • 相关阅读:
    编译无法加载主类的问题
    筛斗数据如何通过先进技术实现数据价值的深度挖掘
    el-dialog修改默认内边距el-dialog__body解决方案
    第1章-数据结构与算法是什么
    使用MySQL进行图像数据存储与处理的实践经验
    计算机毕业设计node+vue基于微信小程序的货物管理系统的设计与实现
    Java教程:一文详解函数式编程
    【云原生】Docker部署数据库的持久化
    Airsonic反向代理问题的解决办法
    蓝牙技术|物联网的可穿戴设备新工作模式,蓝牙BLE助力新工作模式
  • 原文地址:https://blog.csdn.net/myfather103/article/details/82623557