Talk C Chestnut Bar (155th: C-language instance-Get socket communication address)

Source: Internet
Author: User

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

Crossing, the Zhanghuizhong in front of US describes the system calls associated with socket communication addresses. It's not our style to say it or not, and today I'm going to show you how to use these system calls to get the communication address of the socket using the specific code.

The following is a detailed procedure for using the function and its code. I show it to you in code + comments to make it easier for everyone to understand the use of the function.

Char* Host ="localhost";//1. Define a variable to hold the host nameChar* Server ="http";//2. Define a variable to hold the service namestructAddrinfo hints;//3. Defining a template and initializing the templatememset(&hints,0,sizeof(structaddrinfo)); hints.ai_flags = Ai_addrconfig | ai_canonname;hints.ai_family = Af_unspec;hints.ai_socktype = Sock_stream;getaddrinfo (host,server,&hints,& RES);//4. Getting information about the socket communication addressPrint_family (P_addr_info);//5. Displaying information about the socket communication addressPrint_type (P_addr_info);//These functions are defined by themselvesPrint_protocol (P_addr_info);        Print_host_name (P_addr_info);        Print_ipaddress (P_addr_info); Print_port (P_addr_info); Freeaddrinfo (res);//6. Releasing a socket address link

Crossing, the above is the core code, the complete code put in my resources, you can click here to download the use.

In the code, we used a new function: inet_ntop() The function is mainly used to translate the binary format IP address into our common dotted decimal format (in the form of: 127.0.0.0), so that we can read the IP address easily. Similar conversions have a port number, and we use ntohs() functions to convert the port number from the network order to the local byte order.

In the code we used the local host, whose hostname is: localhost . The IP address of the host: 127.0.0.1 . You can also use other host addresses, for example, we use the Sina hostname in our code: "www.sina.com" To do an example, and then get its IP address and other socket information.

The following is the results of the operation of the program, please refer to:

print   Information  of  socket  [family]  af_inet  [type]    Span class= "Hljs-tag" >sock_stream  [protocol]  ipproto_tcp   [host name]  localhost  [ip Address]  127.0  .0  .1  [port]  +  
print   Information  of  socket  [family]  af_inet  [type]    Span class= "Hljs-tag" >sock_stream  [protocol]  ipproto_tcp   [host name]  tucana  .sina  .com  .cn  [ip Address]  111.161  .78  .250  [port]  +  

As you can see from the running results above, the port numbers they output are the same, mainly because the service that we set up the socket in the template is HTTP, which is the Web service we use when we surf the Internet, and its default port number is 80. You can change the template on your own, so that you can output other results.

Ladies and gentlemen, here's an example of getting a socket address crossing. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (155th: C-language instance-Get socket communication address)

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.