关于AF_INET和PF_INET[转]

本文深入探讨了AF_INET和PF_INET宏定义在Windows和Unix/Linux系统中的应用与区别,提供了关于socketpair和socket函数中domain参数使用的建议,并解释了这些概念在实际编程中的应用。

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

AF 表示ADDRESS FAMILY 地址族
PF
表示PROTOCL FAMILY 协议族
但这两个宏定义是一样的
所以使用哪个都没有关系

Winsock2.h
#define AF_INET 0
#define PF_INET AF_INET

 

所以在windows中AF_INET与PF_INET完全一样

 

而在Unix/Linux系统中,在不同的版本中这两者有微小差别

对于BSD,AF,对于POSIXPF

 

 

 

在函数socketpair与socket的domain参数中有AF_UNIX,AF_LOCAL,AF_INET,PF_UNIX,PF_LOCAL,PF_INET.
这几个参数有AF_UNIX=AF_LOCAL, PF_UNIX=PF_LOCAL, AF_LOCAL=PF_LOCAL, AF_INET=PF_INET.
 **建议:对于socketpair与socket的domain参数,使用PF_LOCAL系列,
而在初始化套接口地址结构时,则使用AF_LOCAL.
例如:
     z = socket(PF_LOCAL, SOCK_STREAM, 0);
     adr_unix.sin_family = AF_LOCAL;
 
 
========================================================================================================================================================================

Linux网络编程学习笔记1:AF_INET  

Linux网络编程 |  

1.知名达义:

AF = Address Family (地址族)

INET = Internet

2.身边的例子: 
我们一般的英特网局域网用的就是这个,AF_INET只是一个标识而已。 
定义是这样的 #define AF_INET         2               // internetwork: UDP, TCP, etc.  
此外还有IPX网络,红警用的就是IPX。
 #define AF_IPX          AF_NS           // IPX protocols: IPX, SPX, etc. 
还有ATM网络,就不一一列举了。 这些都定义在了winsock2.h文件中 
3.文档解释:
 
    
socket(AF_INET,SOCK_STREAM,0) 
c++ socket 的这个固定格式是起什么作用
 
 
生成一个TCP的socket  Function: int socket (int namespace, int style, int protocol)  
This function creates a socket and specifies communication style style, which should be one of the socket styles listed in 16.2 Communication Styles. 
The namespace argument specifies the namespace; it must be PF_LOCAL (see section 16.5 The Local Namespace) or PF_INET (see section 16.6 The Internet Namespace). protocol designates the specific protocol (see section 16.1 Socket Concepts); zero is usually right for protocol.  
 The return value from socket is the file descriptor for the new socket, or -1 in case of error. The following errno error conditions are defined for this function:    
EPROTONOSUPPORT  
The protocol or style is not supported by the namespace specified.   EMFILE  The process already has too many file descriptors open. 
  ENFILE  
The system already has too many file descriptors open.  
 EACCES  
The process does not have the privilege to create a socket of the specified style or protocol.  

 ENOBUFS 
 The system ran out of internal buffer space.  The file descriptor returned by the socket function supports both read and write operations. However, like pipes, sockets do not support file positioning operations
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值