Linux High performance reading notebook Universal socket address

Source: Internet
Author: User
Tags get ip

# # #socket网络编程接口
The address of the socket is the structure body sockaddr
The code is as follows
struct sockaddr{
sa_family_t sa_family;
Char sa_data[14];
}
The sa_family member is the address family type (sa_family_t) variable.
Address Family Typeusually with protocol Family TypeCorresponding
1. The corresponding table

Protocol Family Address Table Description
Pf_unix Af_unix Unix Native Domain protocol family
Pf_inet Af_inet Tcp/ipv4 Protocol Family
Pf_inet6 Af_inet6 Tcp/ipv6 Protocol Family

They are defined in the Bits/socket.h header file, the same value, so they are often mixed

Sa_data member for storing socket address values
But the address values of different protocol families have different meanings and lengths.

2. Protocol families and their address values
Protocol Family address value meaning and length
Pf_unix Path name of the file, up to 108 bytes in length
Protocol Family address value meaning and length
Pf_inet 16bit port number and 32bit IPV4 address
Pf_unix Path name of the file, up to 108 bytes in length
Pf_inet6 16bit port number, 32bit stream ID, 128bitipv6 address, 32bit range ID, total 26 bytes

Problem: The 14-byte sa_data cannot accommodate the address values of most protocol families, so Linux defines a new generic socket address structure
struct sockaddr_storage{

    sa_family_t sa_family    unsigned long int __sa_align;    char __ss_padding[128-sizeof(__ss_align)];

}

SSalign: for memory alignment

3. Dedicated socket address
Problem: The above-mentioned general structure is very difficult to use, set and get IP address and port number need to perform tedious bit operation.
Workaround: Linux provides a dedicated socket address structure for each protocol family
3.1
Unix Native Domain protocol family:
struct sockaddr_un{
sa_family_t sin_family; Address Family Af_unix
Char sun_path[108]; File path name

 } The TCP/IP protocol family has two dedicated struct socketaddr_in{sa_family_t sin_family;//Address family af_inet u_    int16_t Sin_port;    Port number, to use the network byte order to represent the struct in_addr sin_addr; Ipv4 address struct} struct in_addr{u_int32_t S_addr;//ipv4 address, to be represented by network byte order} struct SOCKETADDR    _in6{sa_family_t sin6_family;//Address family af_inet u_int16_t Sin6_port;    Port number, to use the network byte order to represent u_int32_t Sin6_flowinfo;    Stream information, set to 0 struct in6_addr sin6_addr;     IPV6 address structure body u_int32_t sin6_scope_id; SCOPE_ID experiment with} struct in6_addr{unsigned char Sa_addr[16];//ipv6 address, to use network byte Order representation} instructions:    A variable with a private address type needs to be converted into a universal socket address type sockaddr (cast) Reason: All sockets become interfaces using the type of address parameter is SOCKADDR 4.IP address conversion function IPv4: Dotted decimal string    Ipv6:16 binary string in_addr_t inet_addr (const char * strptr);    int Inet_aton (const char * cp, struct in_addr * INP); char * INET_NTOA (struct in_addr in);  

Universal socket address for Linux high performance reading notes

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.