Struct sockaddr_in, struct sockaddr, struct in_addr

Source: Internet
Author: User
Tags htons

I. struct sockaddr_in, struct sockaddr, struct in_addr

Struct sockaddr_in, struct sockaddr, struct in_addr, which is a common struct in network programming. Each time they cannot remember what their members are, they need to be checked temporarily for future convenience, here is a summary.

Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14-byte Protocol address */
};

The above is a general socket address, specific to the internet socket, with the following structure, the two can be type conversion
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* Internet address */
Unsigned char sin_zero [8];/* the same length as struct sockaddr */
};

Struct in_addr is a 32-bit IP address.
Struct in_addr {
Unsigned long s_addr;
};

Yes
Struct in_addr {
Union {
Struct {u_char s_b1, s_b2, s_b3, s_b4;} s_un_ B;
Struct {u_short s_w1, s_w2;} s_un_w;
U_long s_addr;
} S_un;
};
Use u_long htonl (u_long hostlong); to convert the host's byte sequence to the TCP/IP network's byte sequence.
Use u_short htons (u_short hostshort) to convert host byte to TCP/IP network byte.

Struct in_addr {

In_addr_t s_addr;

};

Struct in_addr
A 32-bit IPv4 address.

In_addr_t is generally a 32-bit unsigned long.

Each 8 bits represent a value in an IP address bit.

For example, 192.168.3.144 is recorded as 0xc0a80390, where C0 is 192, A8 is 168, 03 is 3, and 90 is 144.

During printing, you can call the inet_ntoa () function to convert it to Char.
* Type.

The common usage is:
Int sockfd;
Struct sockaddr_in my_addr;
Sockfd = socket (af_inet, sock_stream, 0);/* do some error checks! */

My_addr.sin_family = af_inet;/* Host byte order */
My_addr.sin_port = htons (myport);/* short, network byte order */
My_addr.sin_addr.s_addr = inet_addr ("192.168.0.1 ″);

Bzero (& (my_addr.sin_zero), 8);/* zero the rest of the struct */
/* Do not forget to perform an error check for BIND :*/
BIND (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr ));

2. inet_addr, inet_aton, and inet_ntoa

Inet_addr: converts a network address to a binary number, and the returned IP address is in the network order. Function prototype: Unsigned long in inet_addr (const char * CP)

Inet_aton: converts a network address to a binary number of the network. The difference between inet_addr and inet_addr is that the result is not returned, but saved in the in_addr struct referred to by the form parameter P _ ADDR. Function prototype: int inet_aton (cont char * CP, struct in_addr * indium)

Inet_ntoa: Convert the binary number of the network to the network address. The prototype of the function is char * inet_ntoa (struct in_addr in)

III,
There are two updated functions: inet_pton and inet_ntop. These two functions can process ipv4 and ipv6. The prototype is as follows:

int inet_pton(int af, const char *src, void *dst);
This function is used to convert the string to the network address. The first parameter AF is the address family. After the conversion, inet_ton in DST is an extension of inet_addr. The following multi-address families are supported: af_inet: SRC is the first address (DDD. ddd. ddd. in DDD format), the function converts the address to the in_addr struct, and copies af_inet6: RC in * DST to the IPv6 address ,, the function converts the address to the struct of in6_addr and copies it to * DST. If the function fails, a negative value is returned and errno is set to eafnosupport, if the format of the address family specified by AF is incorrect, the function returns 0. The inverse conversion prototype of the function inet_ntop is as follows:
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
This function is used to convert the binary structure of the network to the address of the ASCII type. The parameter serves the same purpose as the preceding one, but the sock parameter is added.

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.