Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

General socket address for linux High performance Reading Notes

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

# socket network programming interface

The address of socket is the structure sockaddr

The code is as follows

Struct sockaddr {

Sa_family_t sa_family

Char sa_data [14]

}

Sa_family members are address family type (sa_family_t) variables.

The address family type usually corresponds to the protocol family type

1. Corresponding table between the two

Protocol family address table describes PF_UNIXAF_UNIXUNIX local domain protocol family PF_INETAF_INETTCP/IPv4 protocol family PF_INET6AF_INET6TCP/IPv6 protocol family

The two are defined in the bits/socket.h header file and have the same value, so they are often mixed.

Sa_data members are used to store socket address values

But the address values of different protocol families have different meanings and lengths.

two。 Protocol family and its address value

Protocol family address value meaning and length PF_UNIX file pathname, length up to 108byte protocol family address value meaning and length PF_INET16bit port number and 32bit IPv4 address PF_UNIX file pathname, length up to 108byte PF_INET616bit port number, 32bit stream ID, 128bitIPv6 address, 32bit range ID, a total of 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. Private socket address

Problem: the above general structure is not easy to use, and setting and obtaining IP addresses and port numbers requires tedious bit operations.

Solution: linux provides a special socket address structure for each protocol family

3.1

UNIX Local Domain Protocol Family:

Struct sockaddr_un {

Sa_family_t sin_family; / / address family AF_UNIX

Char sun_path [108]; / / File pathname

} 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 numbers. The network byte order should be used to represent the struct in_addr sin_addr; / / Ipv4 address structure} struct in_addr {u_int32_t s_addr. / / IPv4 address, to represent} struct socketaddr_in6 {sa_family_t sin6_family; / / address family AF_INET u_int16_t sin6_port; / / port number in network byte order, and u_int32_t sin6_flowinfo; / / stream information in network byte order, set to 0 struct in6_addr sin6_addr / / Ipv6 address structure u_int32_t sin6_scope_id; / / scope_id Experimental} struct in6_addr {unsigned char sa_addr [16] / / IPv6 address To express in network byte order} instructions: variables of all private address types need to be converted to generic socket address type sockaddr (forced conversion) reason: all socket become the type of address parameters used by the interface is sockaddr 4.IP address translation 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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report