Sockets
Description
A socket represents an endpoint of a communications channel. The socket API places few limitations on the nature of this channel, and this has made it possible for many different types of computer network to be accessed through a single set of library functions.
When a socket is created it is given a domain, which defines the type of address (and hence the type of network) that the socket can be
bound to. Examples include AF_INET
for IPv4, AF_INET6
for IPv6, or
AF_UNIX
for a UNIX-domain socket (used for inter-process communication on the local machine).
Sockets also have a type, typically SOCK_STREAM
(for connection-oriented protocols like TCP that carry a stream
of bytes) or SOCK_DGRAM
(for protocols like UDP that provide a datagram service).
Once created, a stream socket can be instructed either to initiate an outbound connection (using the connect
function)
or to wait and listen for inbound connections (using the bind
, listen
and
connect
functions). Datagram sockets can be used immediately without establishing a connection.
microHOWTOs
- Capture Ethernet frames using an AF_PACKET ring buffer in C
- Capture Ethernet frames using an AF_PACKET socket in C
- Establish a TCP connection in C
- Listen for and accept TCP connections in C
- Listen for and receive UDP datagrams in C
- Send a UDP datagram in C
- Send an arbitrary Ethernet frame using an AF_PACKET socket in C
- Send an arbitrary IPv4 datagram using a raw socket in C
Further reading
- W Richard Stevens, Bill Fenner and Andrew M Rudoff, UNIX Network Programming, Volume 1: The Sockets Networking API, Addison Wesley, 2003
- socket, Base Specifications, Issue 7, The Open Group, 2008
- connect, Base Specifications, Issue 7, The Open Group, 2008
- listen, Base Specifications, Issue 7, The Open Group, 2008