Beej's Guide Network to Programming series serialization 08

Source: Internet
Author: User

5. Call/capture system functions
In this part, we use system functions to access the network. They are in the Unix header file, and any operating systems that support socket apps (BSD, Windows, Linux, Mac, etc ). When you call their intermediate functions, the kernel will automatically help you complete all the work.

Most of them are stuck in these system functions. Here, you may find that there is no man available! Well, to help me adapt to it smoothly, I will explain them in the order in which your program calls system functions.

Then, you need some milk and cookies and provide you with some sample code. Others are your courage and high courage! You will find that it turned out to be so happy!

(Please note that for the sake of concise code, there is no error check included. When the getaddrinfo () function is successfully called, a valid linked list is returned ). Some programs use the original address (properly addressed --- can also translate the prototype address .)

5.1. getaddrinfo () --- prepare for running
This is a real unknown function. Although it does not have many parameters, it is very easy to use. It simplifies the creation of an address structure.

A small history: it used to be, you will use the so-called gethostbyname () function for DNS lookup. Then you manually load some information to the SOCKADDR_IN structure for your call.

Fortunately, this is no longer necessary. (If you want to write code that works on IPv4 and IPv6, it is not advisable to use the Code mentioned above !) In this modern age, you already have the getaddrinfo () function and encapsulated a lot of things for you, including DNS and service Name Lookup, in addition to the structure you need!

Let's take a look!

# Include <sys/types. h>

# Include <sys/socket. h>

# Include <netdb. h>

 

Intgetaddrinfo (const char * node, // e.g. www. example. comor IP

Const char * service, // e.g. "http" or port number

Const struct addrinfo * hints,

Struct addrinfo ** res );

In this function, there are three input parameters and one pointer to the linked list as the return value res.

The input parameter node is the host name or IP address to be connected.

The next input parameter "service" is the port number, such as "80", or other specific service names, such as "http"

, "Ftp", "telnet", "smtp", and so on.

Finally, the input parameter hints points to an addrinfo structure that you have filled in.

The following example shows how the server listens on your host IP address and port number 3490. Please note that they have not made any actual listening or network settings; they just set the structure hints and made calls.

 

Int status;

Struct addrinfohints;

Struct addrinfo * servinfo; // will point to theresults

 

Memset (& hints, 0, sizeof (hints); // make sure thestruct is empty

Hints. ai_family = AF_UNSPEC; // don't care IPv4 orIPv6

Hints. ai_socktype = SOCK_STREAM; // TCP stream sockets

Hints. ai_flags = AI_PASSIVE; // fill in myIP for me

 

If (status = getaddreinfo (NULL, "3049", & hints, & servinfo ))! = 0 ){

Fprintf (stderr, "getaddrinfo error: % s \ n", gai_strerror (status ));

Exit (1 );

}

 

// Servinfo nowpoints to a linked list of 1 or more struct addrinfos

 

//... Do everythinguntil you don't need servinfo anymore...

 

//... Do everythinguntil you don't need servinfo anymore...

 

Freeaddrinfo (servinfo); // free the linked-list

 

Note: Because ai_family is set to AF_UNSPEC, it is not related to the version. You can also set an IPv4 address or IPv6 address family.

In addition, you can see the AI_PASSIVE mark here; he told getaddrinfo () to copy the local address to the socket structure. This is good because there is no hard encoding.

Note: The above situation is usually used on the server!

The following is about the client.

Example: connect to port 3049 of www.example.net.

Int status;

Struct addrinfohints;

Struct addrinfo * servinfo; // will point to the results

 

Memset (& hints, 0 sizeof (hints); // make sure the structis empty

Hints. ai_family = AF_UNSPEC; // don't care IPv4 or IPv6

Hints. ai_socktype = SOCK_STREAM; // TCP stream sockets

 

// Get ready toconnect

Status = getaddrinfo (www.example.net, "3049", & hints, & servinfo );

 

// Servinfo nowpoints to a linked list of 1 or more struct addrinfos

 

// Etc.

 

I keep saying that servinfo is a pointer that can point to various addresses. Let's write a simple demo program to display different address information. This program prints the IP address of the specified host on the command line.

Showip. c

List of required header files:

# Include <stdio. h>

# Include <string. h>

# Include <sys/types. h>

# Include <sys/socket. h>

# Include <netdb. h>

# Include <arpa/inet. h>

Flowchart


 

As you can see, the Code calls getaddrinfo () to fill in the linked list pointed by res, and then we can traverse the list and print it on the command line.

Note: IPv6 Testing failed! Please search for available IPv6 sites in China.

 

Now we are under control. We will use getaddrinfo () to get the result. We will pass it to other socket functions and finally establish our network connection! Please continue reading!

 

From the column xiaobin_HLJ80

Related Article

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.