Linux 套接字系统

最后更新: 2022-05-18 17:00:37 +0800 +0800
原文以英文撰写。您正在阅读AI翻译版本。

套接字是表示通信信道一个端点的文件描述符。 程序使用 socket(2) 创建套接字,然后通常对其执行绑定或连接,并使用 read(2)/write(2) 或 send(2)/recv(2)。

c
int sockfd = socket(int domain, int type, int protocol);

域选择地址族,例如 AF_INET、AF_INET6、AF_UNIX 或 AF_NETLINK。类型选择语义,例如 SOCK_STREAM、SOCK_DGRAM 或 SOCK_SEQPACKET;协议通常选择该组合中的具体协议,或者使用 0 选择默认协议。

常见套接字族 #

FamilyAddressCommon Uses
AF_UNIXPathname or abstract nameLocal IPC
AF_INETIPv4 address and portTCP, UDP, raw IP
AF_INET6IPv6 address and portTCP, UDP, raw IPv6
AF_NETLINKKernel protocol and port IDKernel configuration APIs
AF_PACKETInterface and Ethernet protocolLink-layer packet access
AF_VSOCKVM CID and portHost/guest communication

评论

在 GitHub 上阅读或评论(发表评论需要登录)