套接字是表示通信信道一个端点的文件描述符。
程序使用 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 选择默认协议。
常见套接字族 #
| Family | Address | Common Uses |
|---|---|---|
AF_UNIX | Pathname or abstract name | Local IPC |
AF_INET | IPv4 address and port | TCP, UDP, raw IP |
AF_INET6 | IPv6 address and port | TCP, UDP, raw IPv6 |
AF_NETLINK | Kernel protocol and port ID | Kernel configuration APIs |
AF_PACKET | Interface and Ethernet protocol | Link-layer packet access |
AF_VSOCK | VM CID and port | Host/guest communication |
评论
在 GitHub 上阅读或评论(发表评论需要登录)