Linux Socket System

Last update: 2022-05-18 17:00:37 +0800 +0800
Available translations: 日本語 / 中文(简体) / 中文(繁體)

A socket is a file descriptor representing one endpoint of a communications channel. Programs create one with socket(2), then usually bind or connect it and use read(2)/write(2) or send(2)/recv(2).

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

The domain selects an address family such as AF_INET, AF_INET6, AF_UNIX, or AF_NETLINK. The type selects a semantic such as SOCK_STREAM, SOCK_DGRAM, or SOCK_SEQPACKET; the protocol normally selects a concrete protocol within that combination, or 0 selects the default.

Common Socket Families #

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

Comments

Read or comment on GitHub (sign-in required to comment)