Talk C Chestnut Bar (154th: C language Instance--socket communication address system call II)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the socket communication address system Call example, let's go on to this example. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, the communication address system call we introduced last time effectively solves the problem of how data is stored, but there is also a system call that solves this problem, and this time we will introduce the system call: Getaddrinfo.

Function prototypes

The system call from its name is used to obtain the address of the information, where the address is mainly referred to the socket communication address. The address information it obtains does not require a byte-order conversion, and we can use it directly for socket communication. However, we cannot give it a conclusion by just looking at the name, but it can also get the properties (domain, type, and protocol) of the socket and other communication-related information except for the socket address information. The following is the function prototype that the system calls:

      int getaddrinfo(constcharconstchar *service,                       conststruct addrinfo *hints,                       struct addrinfo **res);

Before we introduce this function, let's start with a description of the type used in the function: struct addrinfo, which is defined as follows:

  struct addrinfo {               int              ai_flags;               int              ai_family;               int              ai_socktype;               int              ai_protocol;               socklen_t        ai_addrlen;               struct sockaddr *ai_addr;               char            *ai_canonname;               struct addrinfo *ai_next;           };

The type belongs to the struct type, it has eight members, and we describe these members in turn:

    • The member Ai_flags is used to control the behavior of Getaddrinfo, which is mainly used in the hints parameter of getaddrinfo function;
    • The member ai_family represents the domain of the socket;
    • The member Ai_socktype represents the type of socket;
    • The member Ai_protocol represents the protocol used when the socket communicates;
    • The member Ai_addrlen represents the length of the socket address;
    • The member Ai_addr is a pointer to the address of the socket;
    • The member Ai_canonname is a pointer to the name of the local host;
    • The member Ai_next is a pointer to a linked list that holds other socket address information;

Do you find that some members of these members are not feeling a bit familiar? For example, the family representation of the member ai_family and the Zhanghuizhong before we introduced the socket system call is the same. In fact, in addition to the three members of Ai_flags,ai_canonname and Ai_next, the meanings expressed by several other members are exactly the same as those expressed in the previous Zhanghuizhong introduction of socket system calls. We just need to know what the three new members mean.

function usage

Next we introduce the function: How to use getaddrinfo:

    • The function enters data through its first two parameters, and then processes the data, outputting the data through its fourth parameter when the input data is processed.
    • Its third parameter is can be considered as a template, output data in accordance with the format of the template to output;
    • The first parameter of the function is a pointer to a char type, which holds the hostname or IP address of the input;
    • The second parameter of the function is a pointer to a char type that holds the server name or port number entered;
    • The third parameter of the function is a pointer to the Addrinfo type, which is used to control the format of the output data, which is described in detail later;
    • The fourth parameter of the function is a level two pointer of the addrinfo type, which is used to store the data of the function output, which is described in detail later;
    • This function returns 0 when it runs successfully, returns a value other than 0 when the run fails, and different values represent different types of errors;

This function is relatively cumbersome to use, the trouble is mainly reflected in the parameters, below we detail the last two parameters of the use:

  • The third parameter uses only the first four members of the struct type, and the other members can be set to 0 or null pointers. Here is a collection of values for each member of the parameter, and the meaning of its value:

    • Ai_flags is used to control functions, often using ai_addrconfig to assign values, the value of the return to the local IP address; it has other values, I do not list, you can check the Getaddrinfo manual. (Query command: Man getaddrinfo)
    • Ai_family is used to control the domain that returns the socket in the address information, and everyone knows that the socket's domain determines the address structure of the socket, so it essentially controls the address structure of the socket in the returned data, that is, the structure of the member ai_addr. Its value is af_inet (IPV4 type), Af_inet6 (IPV6 type), and any of the Af_unspec, where af_unspec represents all socket fields.
    • Ai_socktype is used to return the type of socket in the data, and its value is either Sock_stream,sock_dgram and 0. If it has a value of 0, all types of sockets are returned. Ai_protocol is used to control the protocol that returns the socket in the data, often using 0 to assign to it, indicating the use of the default protocol;
  • The fourth parameter, in addition to the first member of the struct member is not used, the other members have corresponding values, these values are the result of the function run, the value of the individual members of the meaning, we introduced the ADDRINFO structure type has been introduced, so this is no longer described here. Let's focus on the last member:

    • If the function returns only one socket address in the data, then its value is a null pointer;
    • If the function returns data with two or more socket addresses, it will point to a linked list;
    • The linked list contains all the key content of the socket address;
    • The pointer is a linked pointer to the linked list, which connects the members of the list through the pointer.
    • The socket address list we just mentioned is dynamically generated by the function, it takes up a certain amount of memory space, and we're using these sockets
      The address requires manual freeing of the memory space used by the list, which can be released using the Freeaddrinfo () function, and the following is its function prototype:
void      freeaddrinfo(struct addrinfo *res);

When using this function, simply pass the last parameter in the Getaddrinfo function to it, and it will automatically release the memory space occupied by the socket address list in the parameter.

Crossing, so far in the function we introduced, the getaddrinfo function is more difficult to use the function, but, through the introduction of this chapter, I believe that we have mastered how to use the method of the function, referring to the poetry of Grandpa Mao, really impregnable pass apocalypse really like Iron, and now move from the beginning of the more AH.

You crossing, the example of socket communication address system call we're going to talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (154th: C language Instance--socket communication address system call II)

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.