Linux Network Programming--the name of the protocol and the type of processing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.