For the convenience of network programming, the system provides a set of functions for querying the values and names of protocols, this article mainly introduces the use of protocol family functions and considerations.
Here's a piece of code that explains how a function is used:
#include <netdb.h>#include <stdio.h> / * Show items for protocol * /voidDisplay_protocol (structProtoent *pt) {inti =0;if(PT) {/ * Valid pointer * / printf("Protocol name:%s,", pt->p_name);/ * The official name of the agreement * / if(pt->p_aliases) {/ * alias is not empty * / printf("Alias name:");/ * Show aliases * / while(Pt->p_aliases[i]) {/ * List not to end * / printf('%s ', Pt->p_aliases[i]);/ * Show current alias * /i++;/ * Next alias * /} }printf(", value:%d\n", Pt->p_proto);/ * Protocol value * /}}intMainintargcChar*argv[]) {inti =0;/ * The protocol name to query * / Const Char*Constprotocol_name[]={"IP","ICMP","IGMP","GGP","Ipencap","St","TCP","EGP","IGP","Pup","UDP","HMP","XNS-IDP","RDP","ISO-TP4","XTP","DDP","IDPR-CMTP","IPv6","Ipv6-route","Ipv6-frag","IDRP","RSVP","GRE","ESP","Ah","Skip","IPV6-ICMP","IPV6-NONXT","Ipv6-opts","RSPF","VMTP","EIGRP","OSPF","ax.25","Ipip","Etherip","EnCap","PIM","Ipcomp","VRRP","L2TP","ISIS","SCTP","FC", NULL}; Setprotoent (1);/ * Do not close the file when using function Getprotobyname/etc/protocols*/ while(Protocol_name[i]!=null) {/ * Not to end of array protocol_name * / structProtoent *pt = Getprotobyname ((Const Char*) &protocol_name[i][0]);/ * Query protocol * / if(PT) {/ * Success * /Display_protocol (PT);/ * Show Protocol items * /} i++;/ * Move to the next of array protocol_name * /}; Endprotoent ();/ * Close file/etc/protocols*/ return 0; }
You can run the program and compare the protocol names and values given by the system.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux Network Programming--the name of the protocol and the type of processing