We are now starting to build the foundation for any network communication. To perform network I/O, the very first thing a process must do is call the socket function. Think of this as installing a telephone line before you can make a call—you need the endpoint created before you can connect to anyone.

1. The Core Logic: socket()
The socket function specifies the type of communication protocol you want to use (e.g., IPv4 with TCP, or IPv6 with UDP).
Function Prototype:
#include <sys/socket.h>
int socket(int family, int type, int protocol);
Returns:
- Success: A non-negative integer, known as the socket descriptor (or
sockfd). - Error:
-1(anderrnois set).
2. Breakdown of Arguments
We must choose these carefully to ensure they match the protocol suite you intend to use.
A. family (Protocol Family)
This specifies the protocol family (often referred to as the domain).
AF_INET: IPv4 protocols (This is the most common one you’ll use).AF_INET6: IPv6 protocols.AF_LOCAL: Unix domain protocols (used for communication between processes on the same host).AF_ROUTE: Routing sockets (interface to the kernel’s routing table).AF_KEY: Key socket (interface to the kernel’s key table for security).
B. type (Socket Type)
This specifies the semantics of communication.
SOCK_STREAM: A stream socket (reliable, two-way connection). This usually maps to TCP.SOCK_DGRAM: A datagram socket (unreliable, connectionless). This usually maps to UDP.SOCK_SEQPACKET: A sequenced packet socket (reliable, connection oriented, preserves message boundaries). Used by SCTP.SOCK_RAW: A raw socket (allows direct access to lower-layer protocols like IP).
C. protocol
This specifies the specific protocol to use.
IPPROTO_TCP: TCP transport protocol.IPPROTO_UDP: UDP transport protocol.IPPROTO_SCTP: SCTP transport protocol.- 0: If you set this to 0, the system selects the default protocol for the given family and type (e.g.,
AF_INET+SOCK_STREAMdefaults to TCP).
3. Valid Combinations (Summary of Figure 4.5)
Not all families and types can be mixed. Here are the valid combinations generally supported:
| Address Family | SOCK_STREAM | SOCK_DGRAM | SOCK_SEQPACKET | SOCK_RAW |
|---|---|---|---|---|
| AF_INET | TCP, SCTP | UDP | SCTP | IPv4 |
| AF_INET6 | TCP, SCTP | UDP | SCTP | IPv6 |
| AF_LOCAL | Yes | Yes | Yes | — |
| AF_ROUTE | — | — | — | Yes |
| AF_KEY | — | — | — | Yes |
(Note: “Yes” indicates a valid combination that doesn’t have a simple acronym like TCP/UDP).
You might see code that uses PF_INET (Protocol Family) instead of AF_INET (Address Family).
The original design intended for a single Protocol Family (PF) to support multiple Address Families (AF). However, this never happened. In modern systems, PF_ values are essentially equal to AF_ values.
5. Code Example
Here is a simple snippet demonstrating how to create a standard TCP socket for IPv4.
#include <sys/socket.h>
#include <netinet/in.h> // for IPPROTO_TCP constants if needed
#include <stdio.h> // for printf/perror
int main() {
int sockfd;
// Create a TCP socket (IPv4, Stream-based, default protocol)
// This is the "telephone" installation phase.
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket error");
return -1;
}
printf("Socket created successfully! Descriptor: %d\n", sockfd);
// Later: connect() or bind() ...
return 0;
}