ADVERTISEMENT
Responsive Ad Unit • Systems Architecture & Low-Level Engineering

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

Client ↓ Connection Request ↓ Server ↓ Data Exchange ↓ Connection Closed

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

#include int main(void) { int sockfd; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd != -1) { /* Socket created successfully */ } return 0; }

The program creates an IPv4 TCP socket using the POSIX socket() function.

9. Interview Questions

  1. What is a socket?
  2. What is Network IPC?
  3. What is the difference between a socket descriptor and a file descriptor?
  4. What does socket() return?
  5. What is the purpose of AF_INET?
  6. When is AF_UNIX used?
  7. What is the difference between TCP and UDP sockets?
  8. Which socket type is connection-oriented?
  9. What is a socket address structure?
  10. 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

Server ↓ socket() ↓ bind() ↓ listen() ↓ accept() ↑ connect() ↓ socket() ↓ Client

The server creates a listening socket before clients establish connections using connect().

17. Practical POSIX Example

#include #include int main(void) { int sockfd; struct sockaddr_in addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd != -1) { bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)); } return 0; }

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

  1. Why is bind() required?
  2. What is the purpose of listen()?
  3. Why does accept() return a new socket descriptor?
  4. What does connect() do?
  5. What is the difference between a listening socket and a connected socket?
  6. What is a port number?
  7. What are well-known ports?
  8. What is the difference between IPv4 and IPv6?
  9. Why does the server call listen() before accept()?
  10. 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

socket() ↓ bind() (Server) ↓ listen() ↓ accept() ↓ send() / recv() ↓ shutdown() ↓ close()

A typical TCP server creates a socket, accepts client connections, exchanges data, and finally closes the socket.

28. Practical POSIX Example

#include int send_message(int sockfd) { const char msg[] = "Hello"; return send(sockfd, msg, sizeof(msg), 0); }

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

  1. What is the purpose of send()?
  2. How does recv() indicate an orderly connection shutdown?
  3. Why is sendto() commonly used with UDP?
  4. What additional information does recvfrom() provide?
  5. What is the purpose of shutdown()?
  6. How is shutdown() different from close()?
  7. When would getsockname() be useful?
  8. What information does getpeername() return?
  9. What is the normal lifecycle of a TCP socket?
  10. Can send() and recv() 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() and accept() implement server-side connection handling.
  • connect() establishes client-side connections.
  • send() and recv() transfer data over connected sockets.
  • sendto() and recvfrom() support datagram communication.
  • setsockopt() and getsockopt() manage socket configuration.
  • TCP provides reliable, ordered communication, while UDP provides connectionless datagram delivery.
  • Portable applications should prefer POSIX socket interfaces.
Knowledge Check

Concept Mastery Assessment — Module 60

Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.

Question 1 of 12
What is the primary architectural purpose of Network IPC - UNIX Domain & Internet Sockets in operating systems?
Explanation
Network IPC - UNIX Domain & Internet Sockets is designed to manage socket(), bind(), listen(), accept(), connect(), af_unix vs af_inet, sock_stream vs sock_dgram, and passing file descriptors over sockets. within the operating system architecture.
Question 2 of 12
Which system interface or data structure is centrally associated with Network IPC - UNIX Domain & Internet Sockets?
Explanation
Under POSIX and Linux, Network IPC - UNIX Domain & Internet Sockets operates through standardized system calls and kernel control structures.
Question 3 of 12
What safety or concurrency constraint must developers consider when implementing Network IPC - UNIX Domain & Internet Sockets?
Explanation
Proper synchronization, avoidance of race conditions, and handling reentrant execution are critical when working with Network IPC - UNIX Domain & Internet Sockets.
Question 4 of 12
When a system call related to Network IPC - UNIX Domain & Internet Sockets fails in C, how does the POSIX runtime conventionally report the failure?
Explanation
Standard POSIX system calls return -1 or NULL on error, assigning an error integer (e.g. EINVAL, EPERM, EAGAIN) to the thread-local errno variable.
Question 5 of 12
What is the primary performance tradeoff associated with Network IPC - UNIX Domain & Internet Sockets?
Explanation
Operations involving Network IPC - UNIX Domain & Internet Sockets balance performance against kernel transition, memory bandwidth, and cache locality tradeoffs.
Question 6 of 12
What security principle governs access to resources in Network IPC - UNIX Domain & Internet Sockets?
Explanation
The kernel validates credentials, effective permissions, and Linux capabilities before granting access to protected operations.
Question 7 of 12
How does execution of Network IPC - UNIX Domain & Internet Sockets transition between User Space and Kernel Space?
Explanation
User space transitions to kernel mode securely via software traps or SYSCALL instructions that switch processor rings to Ring 0.
Question 8 of 12
What happens to resources allocated by Network IPC - UNIX Domain & Internet Sockets when the owning process terminates?
Explanation
Process resources (open fds, memory maps) are automatically reclaimed by the kernel on termination, while kernel-persistent objects survive.
Question 9 of 12
In production Linux server environments, how is Network IPC - UNIX Domain & Internet Sockets optimized for high throughput?
Explanation
Linux optimizes throughput via zero-copy data paths, asynchronous kernel events, and scalable concurrency primitives.
Question 10 of 12
What standard governs the portability of Network IPC - UNIX Domain & Internet Sockets across Unix, Linux, and macOS platforms?
Explanation
IEEE POSIX standards define consistent APIs across Unix-like platforms ensuring portable source code execution.
Question 11 of 12
Which command-line diagnostic tool is most effective for inspecting and troubleshooting Network IPC - UNIX Domain & Internet Sockets at runtime?
Explanation
Utilities like strace, lsof, and the /proc filesystem provide direct visibility into kernel interactions and resource status.
Question 12 of 12
What is a critical production best practice when developing systems software involving Network IPC - UNIX Domain & Internet Sockets?
Explanation
Defensive programming requires explicit error checking on all system operations, proper resource cleanup, and avoiding memory corruption.
0 / 12 (0%)
Great job! Review the concepts above and proceed when ready.
ADVERTISEMENT
Responsive Ad Unit • Technical Courses & Certifications