zoukankan      html  css  js  c++  java
  • 协议名称处理函数xxxprotoxxx()

    为了方便操作,Linux提供了一组用于查询协议的值及名称的函数。

    xxxprotoxxx()函数:

    image

    上面的函数对文件/etc/protocols中的记录进行操作,文件中记录了协议的名称、值和别名等值,与struct protoent的定义一致。

    image

     

    使用协议族函数的例子:

    首先,使用setprotoent(1)打开文件/etc/protocols,然后使用函数getprotobyname()查询函数并显示出来,最后使用函数endprotoent()关闭文件/etc/protocols。

    #include <netdb.h>
    #include <stdio.h>
    
    void
    display_protocol(struct protoent *pt)
    {
        int i = 0;
        
        if(pt)
        {
            printf("protocol name: %s", pt->p_name);
            if(pt->p_aliases)
            {
                printf("alias name:");
                while(pt->p_aliases[i])
                {
                    printf("%s ", pt->p_aliases[i]);
                    i++;
                }
            }
            printf(", value: %d
    ", pt->p_proto);
        }
    }
    
    
    int
    main(int argc, char **argv)
    {    
        int i = 0;
        
        const char *const protocol_name[] = { "ip", "icmp", "tcp", "udp", NULL };
        
        setprotoent(1);
        while(protocol_name[i] != NULL)
        {
            struct protoent *pt = getprotobyname((const char *)&protocol_name[i][0]);
            if(pt)
            {
                display_protocol(pt);
            }
            i++;
        }
        endprotoent();
        return(0);
    }

    运行结果:

    image

  • 相关阅读:
    let 和 const 命令
    python连接oracle
    Python中小整数对象池和大整数对象池
    前端内容流程导图
    bootstrap插件的一些常用属性介绍
    bootstrap的引入和使用
    Linux 重定向
    Mongodb 备份 数据导出导入
    Mongodb 副本集
    Redis 模糊查询删除操作
  • 原文地址:https://www.cnblogs.com/nufangrensheng/p/3588247.html
Copyright © 2011-2022 走看看