Network IPC: Sockets
Sockets provide a standardized mechanism for communication between processes across a network or within the same system. The Berkeley Sockets API, standardized by POSIX, forms the foundation of modern network programming in UNIX-like operating systems.
Learning Objectives
- Understand socket-based communication.
- Learn the client-server communication model.
- Study socket domains.
- Understand socket types.
- Learn the
socket()system call. - Prepare for network programming.
1. What is Network IPC?
Network Interprocess Communication (Network IPC) enables processes running on different computers or on the same computer to exchange data through network protocols. Sockets provide the programming interface used for this communication.
2. What is a Socket?
A socket is an endpoint for bidirectional communication between two processes. Applications use socket descriptors together with POSIX socket functions to establish communication channels.
3. Client-Server Model
In the client-server model, the server waits for incoming requests while clients initiate communication.
4. Socket Domains
| Domain | Description |
|---|---|
AF_INET |
IPv4 Internet communication. |
AF_INET6 |
IPv6 Internet communication. |
AF_UNIX |
Local interprocess communication. |
5. Socket Types
| Type | Description |
|---|---|
SOCK_STREAM |
Reliable byte-stream communication (TCP). |
SOCK_DGRAM |
Connectionless datagram communication (UDP). |
SOCK_SEQPACKET |
Sequenced reliable packet communication. |
SOCK_RAW |
Raw network access. |
6. socket()
The POSIX socket() function creates a new socket
and returns a socket descriptor that is used in subsequent
network operations.
| Prototype |
|---|
int socket(int domain,
int type,
int protocol);
|
7. Socket Address Structures
| Structure | Purpose |
|---|---|
sockaddr |
Generic socket address. |
sockaddr_in |
IPv4 socket address. |
sockaddr_in6 |
IPv6 socket address. |
sockaddr_un |
UNIX domain socket address. |
8. Practical POSIX Example
The program creates an IPv4 TCP socket using the POSIX
socket() function.
9. Interview Questions
- What is a socket?
- What is Network IPC?
- What is the difference between a socket descriptor and a file descriptor?
- What does
socket()return? - What is the purpose of
AF_INET? - When is
AF_UNIXused? - What is the difference between TCP and UDP sockets?
- Which socket type is connection-oriented?
- What is a socket address structure?
- Why are sockets considered an IPC mechanism?
10. bind()
The POSIX bind() function associates a socket
with a local address. For Internet sockets, this address
typically consists of an IP address and a port number.
| Prototype |
|---|
int bind(int sockfd,
const struct sockaddr *addr,
socklen_t addrlen);
|
11. listen()
The listen() function marks a stream socket as
a passive socket that can accept incoming connection
requests.
| Prototype |
|---|
int listen(int sockfd,
int backlog);
|
12. accept()
The accept() function removes the next pending
connection request from the queue and creates a new socket
for communication with the connected client.
The original listening socket continues accepting new
connections.
| Prototype |
|---|
int accept(int sockfd,
struct sockaddr *addr,
socklen_t *addrlen);
|
13. connect()
The connect() function establishes a connection
between a client socket and a remote server socket.
For stream sockets, communication begins after a successful
connection.
| Prototype |
|---|
int connect(int sockfd,
const struct sockaddr *addr,
socklen_t addrlen);
|
14. Port Numbers
A port number identifies a communication endpoint within a host. Combined with an IP address, it uniquely identifies a network service.
| Port Range | Description |
|---|---|
| 0–1023 | Well-known ports. |
| 1024–49151 | Registered ports. |
| 49152–65535 | Dynamic (ephemeral) ports. |
15. IP Addresses
| Version | Description |
|---|---|
| IPv4 | 32-bit Internet address. |
| IPv6 | 128-bit Internet address. |
16. TCP Connection Sequence
The server creates a listening socket before clients
establish connections using connect().
17. Practical POSIX Example
The program creates a TCP socket and associates it with a
local address using bind().
18. Linux Networking Commands
| Command | Description |
|---|---|
ss -t |
Display TCP sockets. |
ss -l |
Display listening sockets. |
hostname |
Display the system hostname. |
hostname -I |
Display IP addresses. |
ip addr |
Display network interfaces. |
19. Interview Questions
- Why is
bind()required? - What is the purpose of
listen()? - Why does
accept()return a new socket descriptor? - What does
connect()do? - What is the difference between a listening socket and a connected socket?
- What is a port number?
- What are well-known ports?
- What is the difference between IPv4 and IPv6?
- Why does the server call
listen()beforeaccept()? - Which side initiates a TCP connection?
20. send()
The POSIX send() function transmits data through
a connected socket.
It is commonly used with connection-oriented sockets such as
TCP sockets.
| Prototype |
|---|
ssize_t send(int sockfd,
const void *buf,
size_t len,
int flags);
|
21. recv()
The POSIX recv() function receives data from a
connected socket.
It returns the number of bytes received or zero when the peer
has performed an orderly shutdown of the connection.
| Prototype |
|---|
ssize_t recv(int sockfd,
void *buf,
size_t len,
int flags);
|
22. sendto()
The sendto() function transmits data to a
specified destination address.
It is commonly used with connectionless sockets such as UDP,
where each message includes the destination address.
23. recvfrom()
The recvfrom() function receives data together
with the address of the sender.
It is commonly used by UDP servers to identify the source of
incoming datagrams.
24. shutdown()
The shutdown() function disables further sending,
receiving, or both on a connected socket without immediately
releasing the socket descriptor.
| Mode | Description |
|---|---|
SHUT_RD |
Disable further receives. |
SHUT_WR |
Disable further sends. |
SHUT_RDWR |
Disable both sending and receiving. |
25. getsockname()
The getsockname() function returns the local
address associated with a socket.
It is useful when the operating system automatically selects
a local port number during connection establishment.
26. getpeername()
The getpeername() function returns the address of
the remote endpoint connected to a socket.
Applications can use this information to identify the peer
participating in the communication.
27. Socket Lifecycle
A typical TCP server creates a socket, accepts client connections, exchanges data, and finally closes the socket.
28. Practical POSIX Example
The program sends a message through an already connected
socket using the POSIX send() function.
29. Linux Networking Commands
| Command | Description |
|---|---|
ss -tan |
Display all TCP sockets. |
ss -uan |
Display all UDP sockets. |
ip route |
Display the routing table. |
ping hostname |
Verify network connectivity. |
hostname |
Display the system hostname. |
30. Interview Questions
- What is the purpose of
send()? - How does
recv()indicate an orderly connection shutdown? - Why is
sendto()commonly used with UDP? - What additional information does
recvfrom()provide? - What is the purpose of
shutdown()? - How is
shutdown()different fromclose()? - When would
getsockname()be useful? - What information does
getpeername()return? - What is the normal lifecycle of a TCP socket?
- Can
send()andrecv()be used with UDP sockets?
31. setsockopt()
The POSIX setsockopt() function configures
options associated with a socket.
These options control communication behavior before or during
network operations.
| Prototype |
|---|
int setsockopt(int sockfd,
int level,
int option_name,
const void *option_value,
socklen_t option_len);
|
32. getsockopt()
The POSIX getsockopt() function retrieves the
current value of a socket option.
Applications use it to inspect the configuration of an
existing socket.
| Prototype |
|---|
int getsockopt(int sockfd,
int level,
int option_name,
void *option_value,
socklen_t *option_len);
|
33. Common Socket Options
| Option | Purpose |
|---|---|
SO_REUSEADDR |
Permit reuse of a local address. |
SO_KEEPALIVE |
Enable periodic connection checking. |
SO_RCVBUF |
Configure receive buffer size. |
SO_SNDBUF |
Configure send buffer size. |
SO_BROADCAST |
Allow datagram broadcast transmission. |
34. TCP vs UDP
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Reliable delivery | No delivery guarantee |
| Ordering | Ordered data | Ordering not guaranteed |
| Speed | Higher protocol overhead | Lower protocol overhead |
| Typical Socket Type | SOCK_STREAM |
SOCK_DGRAM |
35. APUE / POSIX vs Linux
| Interface | Category |
|---|---|
socket() |
POSIX |
bind() |
POSIX |
listen() |
POSIX |
accept() |
POSIX |
connect() |
POSIX |
send() |
POSIX |
recv() |
POSIX |
setsockopt() |
POSIX |
getsockopt() |
POSIX |
| epoll | Linux Extension |
36. Best Practices
- Check the return value of every socket function.
- Close socket descriptors when communication is complete.
- Use appropriate socket domains and types.
- Validate network addresses before use.
- Select TCP or UDP according to application requirements.
- Configure socket options only when needed.
- Prefer standardized POSIX interfaces for portable applications.
37. Module Summary
This module introduced socket-based network programming, including socket creation, address binding, connection establishment, data transfer, socket options, and the differences between connection-oriented and connectionless communication. These interfaces provide the foundation for portable network applications developed using the POSIX sockets API.
38. Key Takeaways
- Sockets provide bidirectional communication endpoints.
socket()creates a socket descriptor.bind()associates a socket with a local address.listen()andaccept()implement server-side connection handling.connect()establishes client-side connections.send()andrecv()transfer data over connected sockets.sendto()andrecvfrom()support datagram communication.setsockopt()andgetsockopt()manage socket configuration.- TCP provides reliable, ordered communication, while UDP provides connectionless datagram delivery.
- Portable applications should prefer POSIX socket interfaces.
Concept Mastery Assessment — Module 60
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.