AF_INET address family

本文详细介绍了AF_INET地址族的概念及应用,包括基于IPv4的socket应用程序如何使用AF_INET地址结构进行通信。文中对比了BSD4.3和BSD4.4/UNIX98规格下sockaddr_in地址结构的不同,并解释了各个字段的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This address family provides Inter Process Communications between processes that run on the same system or on different systems. Addresses for AF_INET sockets are IP addresses and port number. You can specify an IP address for an AF_INET socket either as an IP address, such as 130.99.128.1, or in its 32–bit form, X'82638001'.

For a socket application that uses the Internet Protocol version 4 (IPv4), the AF_INET address family uses the sockaddr_in address structure. When you use _XOPEN_SOURCE macro, the AF_INET address structure changes to be compatible with BSD 4.4/UNIX 98 specifications. For the sockaddr_in address structure, these differences are summarized in the table:

Table 4. Differences between BSD 4.3 and BSD 4.4/UNIX 98 for sockaddr_in address structure
BSD 4.3 sockaddr_in address structure BSD 4.4/UNIX 98 sockaddr_in address structure
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};

Table 5. AF_INET address structure
Address structure field Definition
sin_lenThis field contains the length of the address for UNIX® 98 specifications.
Note:
The: sin_len field is only provided for BSD 4.4 compatibility. It is not necessary to use this field even when using BSD 4.4/UNIX 98 compatibility. The field is ignored on input addresses.
sin_familyThis field contains the address family, which is always AF_INET when TCP or UDP is used.
sin_portThis field contains the port number.
sin_addrThis field contains the Internet address.
sin_zeroThis field is reserved. Set this field to hexadecimal zeros.

 

AF_INET address family sockets can be either connection-oriented (type SOCK_STREAM) or they can be connectionless (type SOCK_DGRAM). Connection-oriented AF_INET sockets use TCP as the transport protocol. Connectionless AF_INET sockets use UDP as the transport protocol. When you create an AF_INET domain socket, you specify AF_INET for the address family in the socket program. AF_INET sockets can also use a type of SOCK_RAW. If this type is set, the application connects directly to the IP layer and does not use either the TCP or UDP transports.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值