cs

作者: EamonXia | 来源:发表于2019-01-23 10:59 被阅读0次

    hostinfo.c

    #include <csapp.h>
    #include <execinfo.h>
    
    void print_stack(); 
    
    int main(int argc, char ** argv)
    {
    #if 0
         /*测试main函数入参*/
        printf("argc = %d\n", argc);
        int counter = 0;
        printf("%p\n", argv);
        printf("%s,%p,%p\n", *argv, *argv, argv[0]);
        printf("%c\n", **argv);
        printf("MAXLINE = %d\n", MAXLINE);
    #endif
        //atexit(print_stack); 
        
        struct addrinfo *p, *listp, hints;
        char bufhost[MAXLINE], bufserv[MAXLINE],bufinetntop[MAXLINE];
        int res, flags;
        struct sockaddr_in *temp;
    
        /*main函数入参校验*/
        if(3 != argc)
        {
            fprintf(stderr,"%s <domin name> <service>\n", argv[0]);
            exit(EXIT_FAILURE);
        }
    
        /*调用getaddrinfo函数,获取*/ 
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_CANONNAME;
        if(0 != (res = getaddrinfo(argv[1], argv[2], &hints, &listp)))
        {
            fprintf(stderr,"%s %s getaddrinfo return errcode is %s\n", argv[0],argv[1], 
                gai_strerror(res));
            exit(EXIT_FAILURE);
        }
       
        flags = NI_NUMERICSERV;
        //flags = 0;
        for(p = listp; p != NULL ; p = p -> ai_next)
        {
            if(0 !=(res = getnameinfo(p -> ai_addr, p -> ai_addrlen,
                bufhost, MAXLINE, bufserv, MAXLINE, flags)))
            {
                fprintf(stderr, "%s %s getnameinfo return error\n");
                exit(EXIT_FAILURE);
            }
            printf("print the args from getnameinfo function:\n");
            printf("host : %s, service : %s\n\n", bufhost, bufserv);
    
            printf("print the ai_canonname and ai_addrlen:\n");
            printf("ai_canonname is %s and ai_addrlen is %d\n\n",p->ai_canonname,p->ai_addrlen);
    
            printf("print the result from p->ai_addr:\n");
            temp = (struct sockaddr_in *)p -> ai_addr;
            struct in_addr tem = temp -> sin_addr;
            inet_ntop(AF_INET, &tem, bufinetntop, MAXLINE);
            uint16_t portnumber = ((struct sockaddr_in *)(p->ai_addr))->sin_port;
            portnumber = ntohs(portnumber);
            printf("p->ai_addr->sin_addr:%s,p->ai_addr->sin_port:%d\n=======================================\n",bufinetntop, portnumber);
        }
        
        
    
        freeaddrinfo(listp);
    
        exit(EXIT_SUCCESS);
        
    }
    
    
    void print_stack()  
    {  
        int size = 16;  
        int i;    
        void *array[16];  
        int stack_num = backtrace(array, size);  
        char **stacktrace = NULL;  
              
        stacktrace = (char**)backtrace_symbols(array, stack_num);  
        printf("==========begin print backtrace================\n");
        for(i = 0; i < stack_num; i++)  
        {  
            printf("%d------>%s\n",i, stacktrace[i]);  
        }  
        free(stacktrace);  
    }  
    

    servermain.c

    #include <csapp.h>
    
    int open_listenfd_created_by_myself(char * hostname, char * port);
    
    int main(int argc, char ** argv)
    {   
        int listenfd, connfd;
        struct sockaddr clientaddr;
        char buf[MAXLINE];
        rio_t rio;
        size_t n;
        
        if(3 != argc)
        {
            fprintf(stderr, "%s <hostname><numeric port>", argv[0]);
            exit(EXIT_FAILURE);
        }
    
        int clientaddrlen = sizeof(struct sockaddr);
        
        listenfd = open_listenfd_created_by_myself(argv[1], argv[2]);
    
        while(1)
        {
            if(0 < (connfd = accept(listenfd, &clientaddr, &clientaddrlen)))
            {
                printf("the connection has builded.\n");
                
                rio_readinitb(&rio, connfd);
    
                while(0 != (n = rio_readlineb(&rio, buf, MAXLINE)))
                {
                    printf("===========\nreceive the\n%sfrom the client.\n============\n",buf);
                }
            }
            Close(connfd);
            break;
        }
        Close(listenfd);
        exit(EXIT_SUCCESS);
    
    }
    
    
    int open_listenfd_created_by_myself(char * hostname, char * port)
    {
        struct addrinfo *p, *plist, hints;
        int res;
        int listenfd;
        struct sockaddr_in6 *temp;
        char bufinetntop[MAXLINE];
    
    
        
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_flags = AI_ADDRCONFIG;
        hints.ai_flags |= AI_NUMERICSERV;
        //hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        
        if(0 != (res = getaddrinfo(hostname, port, &hints, &plist)))
        {
            fprintf(stderr, "call getaddrinfo() function failed the errcode is %d.\n",gai_strerror(res));
            exit(EXIT_FAILURE);
        }
        
        for(p = plist; p; p = p->ai_next)
        {
            if(0 >= (listenfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)))
            {
                continue;
            }
    
            if(0 !=(res = bind(listenfd, p->ai_addr, p->ai_addrlen)))
            {
                continue;
            }
            /*
            temp = (struct sockaddr_in6 *)p -> ai_addr;
            struct in6_addr tem = temp -> sin6_addr;
            inet_ntop(AF_INET6, &tem, bufinetntop, MAXLINE);
            uint16_t portnumber = ((struct sockaddr_in6 *)(p->ai_addr))->sin6_port;
            portnumber = ntohs(portnumber);
            printf("p->ai_addr->sin_addr:%s,p->ai_addr->sin_port:%d\n",bufinetntop, portnumber);
            */
    
    
            
            if(0 == listen(listenfd , 1024))
            {
                freeaddrinfo(plist);        
                return listenfd;
            }
        }
        Close(listenfd);
        freeaddrinfo(plist);
        exit(EXIT_FAILURE);
    }
    
    

    clientmain.c

    #include <csapp.h>
    #include <execinfo.h>
    
    int open_clientfd_created_by_myself(char * hostname, char * port);
    
    
    int main(int argc, char ** argv)
    {   
        int res, clientfd;
        char buf[MAXLINE];
        
        if(3 != argc)
        {
            fprintf(stderr, "%s <server host name><numeric port number>\n",argv[0]);
            exit(EXIT_FAILURE);
        }
    
        clientfd = open_clientfd_created_by_myself(argv[1], argv[2]);
     
        while(fgets(buf ,MAXLINE, stdin)!=NULL)
        {
            rio_writen(clientfd, buf, strlen(buf));
        }
        Close(clientfd);
        exit(EXIT_SUCCESS);
        
    }
    
    
    int open_clientfd_created_by_myself(char * hostname, char * port)
    {
        int clientfd;
        int res, flags;
        struct addrinfo  hints, *p, * plist;
        char bufhost[MAXLINE], bufport[MAXLINE];
        char bufinetntop[MAXLINE];
    
        struct sockaddr_in6 * temp;
        
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_flags = AI_NUMERICSERV;
        hints.ai_flags |= AI_ADDRCONFIG;
        hints.ai_socktype = SOCK_STREAM;
        //hints.ai_family = AF_UNSPEC;
        if(0 != (res = getaddrinfo(hostname, port, &hints, &plist)))
        {
            fprintf(stderr, "call getaddrinfo func failed and the error code is %d\n",gai_strerror(res));
            exit(EXIT_FAILURE);
        }
        if(NULL == plist)
        {
            fprintf(stderr, "NULL == plist\n");
            exit(EXIT_FAILURE);
        }
    
        for(p = plist; p; p=p->ai_next)
        {   
            flags = 0;
            if(0 == (res=getnameinfo(p->ai_addr, p->ai_addrlen, bufhost, MAXLINE, bufport, MAXLINE, flags)))
            {
                printf("server hostname is %s, and the server port is %s\n", bufhost, bufport);
            }
            /*
            temp = (struct sockaddr_in6 *)p -> ai_addr;
            struct in6_addr tem = temp -> sin6_addr;
            inet_ntop(AF_INET6, &tem, bufinetntop, MAXLINE);
            uint16_t portnumber = ((struct sockaddr_in6 *)(p->ai_addr))->sin6_port;
            portnumber = ntohs(portnumber);
            printf("p->ai_addr->sin_addr:%s,p->ai_addr->sin_port:%d\n",bufinetntop, portnumber);
            */
    
            if(0 >= (clientfd = socket(p->ai_family ,p->ai_socktype,p->ai_protocol)))
            {
                continue;
            }
    
            if(0 == (res = connect(clientfd, p -> ai_addr, p->ai_addrlen)))
            {
                freeaddrinfo(plist);
                return clientfd; 
            }
            printf("res = %d,p->ai_addrlen=%d\n",res,p->ai_addrlen);
            Close(clientfd);
        }
        freeaddrinfo(plist);
        fprintf(stderr, "call open_clientfd_created_by_myself() function failed.\n");
        exit(EXIT_FAILURE);
    }
    
    

    相关文章

      网友评论

          本文标题:cs

          本文链接:https://www.haomeiwen.com/subject/colvdqtx.html