Talk C Chestnut Bar (151th: C language Instance--socket mailing address)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the socket Communication interface example, this time we are talking about the socket address. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we need to know the address of the communication when we use the socket, which is like having an email address when we send an e-mail. In fact, we introduced the socket communication interface mentioned the address of the communication, but no detailed introduction, then there are customer questions. Today we will detail the communication address of the socket.

Socket communication address is mainly used to identify the communication between the two sides, in the view of the program, is used to identify the program in the communication of the computer and its process. A communication address lets you know which computers are communicating and even know that a process in the computer is communicating. For example, which computer the data is sent from, and which computer is ready to send it to.

We use common e-mails to make comparisons, we can know who sent the email by the sender's address of the email, or we can know who will receive the email through the recipient's address. The email address here is like a mailing address, and the addressee and sender are like the computers in the communication. Called the computer, we feel a bit chaotic, in order to facilitate everyone to distinguish the computer, usually, we send the data to the computer called the client, the computer receiving the data called the server. Through the address of the communication, we can know the client and server information, or more precisely, we can know which clients and which servers to communicate.

The socket's communication address is in a fixed format, just like the email address we use. For example [email protected] is an e-mail address that always uses the @ symbol, preceded by the message name, followed by the domain name of the mail service provider. The address of the socket also has a fixed format, but the format of the communication address is related to the domain of the socket, different communication domains have different communication addresses. Next we do the introduction:

Af_unix Domain Communication address format

This type of communication is primarily the communication between the different processes of the same computer, so the communication address is essentially a file path, except that the file is not an ordinary file, but a file specifically designed to communicate with the socket type.

The following is a detailed definition of the type address format, which is located ../include/uapi/linux/un.h in the file

#define UNIX_PATH_MAX   108struct sockaddr_un {        /* AF_UNIX */        char sun_path[UNIX_PATH_MAX];   /* pathname */};

As you can see from the type definition above, the type is a struct type and the struct contains two members:

    • The sun_family member represents the domain of the socket communication;
    • The Sun_path member represents a file path, and the path length is defined as 108 characters.

In addition, we can see from this definition that the Af_unix domain's communication address is implemented through a file address.

Af_inet Domain Communication address format

This type of communication is primarily communication between different computers in the network, so the communication address is essentially a computer name and a port number. Computers can be found by computer name, usually by using the computer's IP address instead of the computer name to differentiate the computers on the network.

The port number is used to specify a specific process for the computer. For example, we access the network through port 80th of the computer, which is actually communicating with the process in the Web process using the computer and the network server. In short, for the af_inet domain's communication address, we can generalize its nature to the IP address + port number.

The following is a detailed definition of the type address format, which is located ../include/uapi/linux/in.h in the file

structsockaddr_in {__kernel_sa_family_t sin_family;/ * Address family * /__be16 Sin_port;/ * Port number * /  structIN_ADDR sin_addr;/ * Internet address * /  / * Pad to size of ' struct sockaddr '. * /  unsigned Char__PAD[__SOCK_SIZE__-sizeof( Short int) -sizeof(unsigned  Short int) -sizeof(structIN_ADDR)];};

As you can see from the type definition above, the type is a struct type and the struct contains four members:

    • The sun_family member represents the domain of the socket communication;
    • The SIN_ADDR member represents the IP address of the computer;
    • The Sin_port member represents the port number of the computer;
    • The __pad member represents the length of the address;

Also, from this definition we can see that the Af_inet domain's communication address is essentially a combination of the computer's IP address and the computer's port number.

Common address format for communications

The communication address of the socket also has a common address format, which is designed to convert different address formats into common address formats to facilitate our use in communication interfaces. The following is the definition of the format, which is located socket.h in the file:

struct sockaddr {        sa_family_t     sa_family;      /* address family, AF_xxx       */        char            sa_data[14];    /* 14 bytes of protocol address */};

As you can see from the definition above, it is a struct type and has two members:

    • The sa_family member represents the domain of the communication;
    • The Sa_data member represents the correspondence address;

Crossing, we have introduced so much about the content of the correspondence address, is to let everyone understand the format of the communication address, when we use the communication address in the program, we can be based on the format of the communication address to fill in the communication address. When using sockets to communicate, we can communicate directly with the already filled communication address.

Crossing, here's an example of the socket communication address. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (151th: C language Instance--socket mailing 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.