• sock_ntop.c


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <sys/un.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    
    char *sock_ntop(const struct sockaddr *sa, socklen_t salen) {
        
        char portstr[8];    
        static char str[128];
       
        switch (sa->sa_family) {
            case AF_INET: {
                struct sockaddr_in    *sin = (struct sockaddr_in *) sa;
    
                if (inet_ntop(AF_INET, &sin->sin_addr, str, 
                    sizeof(str)) == NULL) {
                    return(NULL);
                }
                if (ntohs(sin->sin_port) != 0) {
                    snprintf(portstr, sizeof(portstr), ":%d", 
                        ntohs(sin->sin_port));
                    strcat(str, portstr);
                }
                return(str);
            }
            case AF_INET6: {
                struct sockaddr_in6    *sin6 = (struct sockaddr_in6 *) sa;
    
                str[0] = '[';
                if (inet_ntop(AF_INET6, &sin6->sin6_addr, str + 1, 
                    sizeof(str) - 1) == NULL) {
                    return(NULL);
                }
                if (ntohs(sin6->sin6_port) != 0) {
                    snprintf(portstr, sizeof(portstr), "]:%d",
                        ntohs(sin6->sin6_port));
                    strcat(str, portstr);
                    return(str);
                }
                return (str + 1);
            }
            case AF_UNIX: {
                struct sockaddr_un    *unp = (struct sockaddr_un *) sa;
    
                if (unp->sun_path[0] == 0) {
                    strcpy(str, "(no pathname bound)");
                } else {
                    snprintf(str, sizeof(str), "%s", unp->sun_path);
                }
                return(str);
            }
            default: {
                snprintf(str, sizeof(str), "sock_ntop: unknown AF_xxx: %d, len %d",
                     sa->sa_family, salen);
                return(str);
            }
        }
        return (NULL);
    }

    函数功能:从套接字地址结构中取出IP地址和端口号,返回值为字符串【IP:端口号】的地址指针
  • 相关阅读:
    堆排序
    2019晋城一中开放日
    严格次小生成树
    遥远的国度
    noip2018游记
    Luogu1736 创意吃鱼法
    P3958 奶酪
    Luogu3385 负环
    Luogu1040 加分二叉树
    Luogu1007 独木桥
  • 原文地址:https://www.cnblogs.com/soldierback/p/10667266.html
走看看 - 开发者的网上家园