Linux下网络相关结构体 struct protoent
来源:互联网 发布:贝叶斯算法 spark 编辑:程序博客网 时间:2024/06/05 04:53
参考书籍:《UNIX环境高级编程》
参考连接:
http://www.cnblogs.com/benxintuzi/p/4589819.html
一、简介
sutuct protoent主要用于提供协议名字和协议号
结构体如下:
struct protoent { char *p_name; char **p_aliases; int p_proto;};
1.p_name
表示的是协议规范名。
2.h_aliases
表示的是主机的别名
3.p_proto
协议号
二、代码展示
1)相关函数
struct protoent *getprotoent(void);void setprotoent(int stayopen);void endprotoent(void);struct protoent *getprotobyname(const char *name);struct protoent *getprotobynumber(int proto);
2)getprotobyname
根据协议名字然后匹配“/etc/protocols”,匹配成功,返回struct protoent指针,失败返回空
1.代码
#include <netdb.h>#include <stdio.h>int main(){ struct protoent* proto = NULL;proto = getprotobyname("udp");if (proto != NULL){printf("proto name: %s\n", proto->p_name);printf("alias name: %s\n", *proto->p_aliases);printf("proto number: %d\n", proto->p_proto);}return 0;}
2.运行
$ ./testproto name: udpalias name: UDPproto number: 17
3)参考如下代码,读取“/etc/protocols”全部信息,并将其打印出来
#include <netdb.h>#include <stdio.h>void printproto(struct protoent* proto){ char** p = proto->p_aliases; printf("proto name: %s\n", proto->p_name); while(*p != NULL) { printf("alias name: %s\n", *p); p++; } printf("proto number: %d\n", proto->p_proto);}int main(){ struct protoent* proto = NULL; setprotoent(1); while((proto = getprotoent()) != NULL) { printproto(proto); printf("\n"); } endprotoent(); return 0;}
2.运行
$ ./testproto name: ipalias name: IPproto number: 0proto name: hopoptalias name: HOPOPTproto number: 0proto name: icmpalias name: ICMPproto number: 1proto name: igmpalias name: IGMPproto number: 2proto name: ggpalias name: GGPproto number: 3proto name: ipencapalias name: IP-ENCAPproto number: 4proto name: stalias name: STproto number: 5proto name: tcpalias name: TCPproto number: 6proto name: egpalias name: EGPproto number: 8proto name: igpalias name: IGPproto number: 9proto name: pupalias name: PUPproto number: 12proto name: udpalias name: UDPproto number: 17......
阅读全文
0 0
- Linux下网络相关结构体 struct protoent
- struct protoent结构,getprotobyname 及相关函数
- struct protoent函数结构
- Linux下网络相关结构体 struct hostent
- Linux下网络相关结构体 struct netent
- Linux下网络相关结构体 struct servent
- Linux下网络相关结构体 struct addrinfo
- getprotobyname 以及struct protoent结构
- linux下与实践相关的结构体 struct timeb,struct timeval ,struct timezone,struct timespec,struct tm 等
- linux下与时间相关的结构体 struct timeb,struct timeval ,struct timezone,struct timespec,struct tm 等
- Linux网络编程--struct hostent结构体
- Linux网络编程--struct hostent结构体
- Linux网络编程--struct hostent结构体
- Linux下struct timeval结构体
- 网络编程 struct protoent存储协议编号及对应的名字
- linux 网络编程相关结构体
- Linux下的串口系列---struct termios结构体
- linux下的task struct结构体分析
- SSH 常用操作
- eclipse中Java及html字体颜色的修改
- Jenkins入门系列之—Jenkins安装与配置
- yuv420数据快速裁剪
- OSPF笔记-7
- Linux下网络相关结构体 struct protoent
- Effective C++ 条款36:绝不重新定义继承而来的non-virtual函数
- mapreduce应用-找出扣扣共同好友
- Git相关概念介绍
- 强化学习之Eligibility Traces
- SpringMVC文件上传与下载
- Color a Tree
- java.lang.UnsatisfiedLinkError解决方法汇集
- hadoop文件系统操作