Linux Network programming: gethostbyname (), Getservbyname ()

Source: Internet
Author: User
Tags aliases

Objective

Recently in the Learning Network programming, this article from my classmate Quake East, this article is also to record the study

GetHostByName ()

This function can return the domain name information for a given domain name.

Parameters: Domain Name

return value:

    • The address of a hostent struct (i.e. a pointer to a hostent struct)
    • 0 (if the domain name cannot be resolved to an IP address)

From netdb.h the header file we can find a description of the hostent structure:

struct hostent {    char  *h_name;         /*official host name */    char **h_aliases;     /*other aliases */ int h_addrtype; /*address type */ int h_length; /* address length */ char **h_addr_list; /* list of addresses */ }; #define h_addr h_addr_list[0]

The **h_addr_list in the structure above is a linked list of binary integers that are converted into dotted decimal with the inet_ntop () function when outputting.

Inet_ntop ()

Both Inet_ntop () and Inet_pton () are IP address translation functions that can convert IP addresses between binary integers and dotted decimal. Furthermore, these 2 functions are capable of handling IPv4 and IPv6.

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);

This function transforms the network binary structure to the ASCII type address, the parameters function and Inet_pton the same, just one more parameter socklen_t cnt, he is pointing to the buffer DST size, avoid overflow, if the buffer is too small to store the value of the address, then return a null pointer, and set the errno to ENOSPC.

Now let's do the actual combat:

#Include <stdio.h>#Include <netdb.h>#Include <arpa/inet.h>IntMain(int argc,Char **argv) {struct Hostent *hptr;Char *name, **pptr, str[32];int count =0;if (ARGC <2) {printf"The arguments is not enough!");Return-1; } name = argv[1]; Hptr = gethostbyname (name);if (hptr) {printf"The offical name is%s.\n", hptr->h_name);for (pptr = hptr->h_aliases; *pptr! = NULL; pptr++) {printf"The alias name is%s\n", *pptr); }Switch (hptr->h_addrtype) {Case AF_INET:printf"The address type is af_inet.\n");BreakCase AF_INET6:printf ("The address type is af_inet6.\n"); Break ; default: Break ;} printf ("The address length is%d bytes.\n", hptr->h_length); For (pptr = hptr->h_addr_list; *pptr! = NULL; pptr++) {count + + +; printf ("The%dth address is%s.\n", Count, Inet_ntop (Hptr->h_addrtype, *pptr, str, sizeof (str))); //The decimal point to be converted to the string is returned in STR, the overflow returns a null pointer}} else { printf ("error!\n");} return 0;}                

Compile run:

Getservbyname

This function returns information about services for a given service name and protocol name.

Parameters: Service Name and protocol name

return value:

    • A pointer to the servent struct
    • Null pointer (Error occurred)

From netdb.h the header file we can find a description of the servent structure:

struct servent {    char   *s_name;       /*official service name */    char  **s_aliases;    /*other aliases */ int s_port; /*port for this service */ char **s_proto; /* protocol to use */ };

The port number in the returned struct is an integer saved in network byte order, and the Ntohs () function is used to convert the integer saved in the host order by the output.

Ntohs ()

Network byte order Nbo: Store in order from high to low, and use uniform network byte order on the network to avoid compatibility issues.

Host byte sequence (hbo,host byte order): Different machine HBO is not the same, related to CPU design, the order of data is determined by the CPU, and operating system independent.

The conversion function between the network byte order and the local byte order:

htonl()--"Host to Network Long"ntohl()--"Network to Host Long"htons()--"Host to Network Short"ntohs()--"Network to Host Short"

Now, let's do the actual combat:

#Include <stdio.h>#Include <netdb.h>IntMain(int argc,char* argv[]) {struct servent *sptr;Char *service, *protocol; if (argc < 3) { printf ("The arguments is not enough!\n"); return-1; service = argv[1]; protocol = argv[2]; sptr = getservbyname (service, protocol); if (sptr) { printf ("The port of service%s using%s protocol is%d.\n", Sptr->s_name, Protocol, Ntohs (SPT R->s_port)); //Convert port values in network byte order to host Order} else { printf ("error!\n");} return 0;}                 

Compile run:

Linux Network programming: gethostbyname (), Getservbyname ()

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.