What is socket?

Source: Internet
Author: User
What is socket?
You often hear people talk about
"Socket", maybe you do not know its exact meaning. Now let me tell you: it uses standard UNIX
The method in which file descriptor communicates with other programs.
What?

Struct sockaddr .. This structure stores socket address information for many types of sockets:
Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14-byte Protocol address */
};
Sa_family can be of various types, but it is "af_inet" in this article ".
Sa_data contains the destination address and port information in the socket. It seems unwise.
To process struct sockaddr, the programmer creates a parallel structure: struct sockaddr_in
("In" stands for "Internet ".)
Struct sockaddr_in {
Short int sin_family;/* Communication type */
Unsigned short int sin_port;/* port */
Struct in_addr sin_addr;/* Internet address */
Unsigned char sin_zero [8];/* the length of the sockaddr structure is the same */
};
With this data structure, you can easily process basic elements of a socket address.

A pointer to the sockaddr_in struct can also be directed to the ADDR struct and replaced. This
In this case, even if socket () wants struct sockaddr *, you can still use struct.
Sockaddr_in. In addition, pay attention to
Sa_family is consistent and can be set to "af_inet ". Finally, sin_port and sin_addr
It must be a network byte order )!
Htons () -- "host to network short"
Htonl () -- "host to network long"
Ntohs () -- "network to host short"
Ntohl () -- "network to host long"
Why sin_addr and sin_port in the data structure struct sockaddr_in?
It needs to be converted to the byte sequence of the network, but does sin_family need it? The answer is: sin_addr and
Sin_port is encapsulated in the IP address and UDP layer of the package respectively. Therefore, they must
Is the byte sequence of the network. However, the sin_family domain is only used by the kernel to determine the number
What type of address is contained in the data structure, so it must be in the local byte order. Meanwhile, sin_family
They are not sent to the network. They can be local bytes.

IP addresses and how to handle them
We are lucky because we have many functions to conveniently operate IP addresses. No
It is necessary to manually calculate them, and there is no need to use the "<" operation to store the growth of the entire font.
First, assume that you already have a sockaddr_in struct INA and you have an IP address
To store the address "132.241.5.10", you need to use the inet_addr () function
Point format to unsigned long integer. The usage is as follows:
Ina. sin_addr.s_addr = inet_addr ("132.241.5.10 ");
Note that the address returned by inet_addr () is already in the network byte format, so you do not need to call
Function htonl ().
We now find that the code snippet above is not very complete because it does not have an error check.
Obviously,-1 is returned when an error occurs in inet_addr. Remember these binary numbers? (No character
-1 only matches the IP address 255.255.255.255! This is the broadcast address! Big mistake
Error! Remember to perform the error check first.
Now you can convert the IP address to a new integer. Is there an alternative?
Can it output an in_addr struct to the point format? In this case, you need to use the Function
Inet_ntoa () ("ntoa" means "network to ASCII"), just like this:
Printf ("% s", inet_ntoa (INA. sin_addr ));
It outputs the IP address. Note that inet_ntoa () treats the struct in-ADDR as
Parameters, not long integer. It also needs to be noted that it returns a character pointing
Pointer. It is a static fixed pointer controlled by inet_ntoa (), so each call
Inet_ntoa (), which overwrites the IP address obtained during the last call.

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.