Network Programming: Sockets & Protocols
Network programming enables communication between processes on local or remote hosts using standard protocols like TCP and UDP via socket abstractions.
Core Principles
- Client-Server Paradigm: Servers provide services and wait for requests; clients consume services and initiate connections.
- Socket Abstraction: A communication endpoint used to speak to other programs using standard Unix file descriptors.
- TCP (Transmission Control Protocol): Connection-oriented, reliable, full-duplex byte stream service.
- UDP (User Datagram Protocol): Connectionless, unreliable datagram delivery service; prioritizes speed over accuracy.
- Encapsulation: Data moves down the protocol stack, adding headers at each layer (e.g., TCP segment, IP datagram, Ethernet frame).
- Port Numbers: 16-bit integers used to differentiate processes; ranges include Well-known (0-1023), Registered (1024-49151), and Dynamic/Private (49152-65535).
Key Terms
- Socket API (SOCK_STREAM/SOCK_DGRAM)::
- : Interface for network communication; Stream for TCP, Datagram for UDP.
- Ephemeral Port::
- : Short-lived port assigned automatically to a client.
- Iterative Server::
- : Server that handles one request at a time (typical for UDP).
- Concurrent Server::
- : Server that spawns a child process or thread for each connection (typical for TCP).
- Demultiplexing::
- : Process of stripping headers as data moves up the protocol stack at the destination.
Pro Tips
- Use /etc/services on your Unix system to look up well-known port mappings.
- TCP uses an 'advertised window' to prevent a fast sender from overwhelming a slow receiver's buffer.
- A socket is identified by an IP address and a port number.
Pitfalls to Avoid
- Missing lectures, labs, or tutorials, as exams are based on all delivery components.
- Committing academic offenses like plagiarism, collusion, or falsification.
- Assuming TCP preserves message boundaries (it is a byte stream service).
- Neglecting to handle retransmissions or flow control in custom protocols.
Myth vs Reality
- UDP is always worse than TCP because it is unreliable.: UDP is preferred for applications where prompt delivery is more critical than guaranteed delivery or ordering.
- All ports are equal.: Ports below 1024 are reserved in Unix and require superuser access.
More like this