Socket Programming Basics 1--hostent, IN_ADDR, gethostbyname, Inet_ntoa

Source: Internet
Author: User

1. struct Hostent structural body

The official name of the H_name address.
H_aliases A pointer to the prestaged name (alias).
H_addrtype address type; Usually a af_inet.
The bit length of the h_length address.
H_addr_list Host network address pointer. Network byte order.
H_ADDR the first address in the h_addr_list.

2. in_addr Structural Body

#include <arpa/inet.h>typedef struct IN_ADDR {          union {                  struct {UCHAR s_b1,s_b2,s_b3,s_b4;} S_un_b;                  struct {USHORT s_w1,s_w2;} S_un_w;                  ULONG s_addr;          } S_un;  #define S_ADDR  S_un. S_ADDR/        * Can is used for most TCP & IP code */  #define S_HOST  S_un. S_UN_B.S_B2    //Host on IMP  #define S_net   S_un. S_UN_B.S_B1    //Network  #define S_IMP   S_un. S_UN_W.S_W2    //Imp  #define S_IMPNO S_un. S_UN_B.S_B4    //Imp #  #define S_LH    S_un. S_UN_B.S_B3    //Logical host  } in_addr, *pin_addr, far *lpin_addr;

3. gethostbyname () function
#include <netdb.h> #include <sys/socket.h>struct hostent *gethostbyname (const char *name);

This function calls into the host name or domain name, such as WWW.GOOGLE.COM,WPC, and so on. The function returns a pointer to the struct hostent struct type, and returns a null if the call fails.

The h_addr_list in the returned struct hostent struct is an array of pointers, and each element in the array can be considered a pointer to the IN_ADDR type. is defined as char** because the hostent structure supports

Multiple address types.

by gethostbyname ("www.baidu.com") the hostent can be printed as follows:

4. Inet_ntoa () function
#include <arpa/inet.h>char *inet_ntoa (struct in_addr);

The function is to convert the address of the network byte order to a band "." The IP address string for the format.

Socket Programming Basics 1--hostent, IN_ADDR, gethostbyname, Inet_ntoa

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.