TCP/IP Networking Fundamentals

Introduction

The Transmission Control Protocol/Internet Protocol (TCP/IP) suite is a foundational set of communication protocols used for the internet and many other computer networks. It defines how data should be packetized, addressed, transmitted, routed, and received. This documentation provides an overview of its core components.

TCP/IP operates on a layered model, conceptually similar to the OSI model, which breaks down network communication into manageable layers, each with specific responsibilities. The most common interpretation includes four layers:

Transmission Control Protocol (TCP)

TCP is a connection-oriented, reliable, and ordered protocol. It ensures that data is delivered accurately and without duplication, even if the underlying network is unreliable. Key features include:

TCP Header Fields (Simplified)


+-----------------+-----------------+-----------------+-----------------+
| Source Port     | Destination Port|                                   |
+-----------------+-----------------+-----------------+-----------------+
| Sequence Number                                       |                 |
+-----------------+-----------------+-----------------+-----------------+
| Acknowledgment Number                                 |                 |
+-----------------+-----------------+-----------------+-----------------+
| Data Offset     | Reserved        | Flags           | Window Size     |
+-----------------+-----------------+-----------------+-----------------+
| Checksum        | Urgent Pointer  | Options (if any)|                 |
+-----------------+-----------------+-----------------+-----------------+
|                 Padding (if any)                                      |
+-----------------+-----------------+-----------------+-----------------+
            

User Datagram Protocol (UDP)

UDP is a connectionless, unreliable, and unordered protocol. It offers lower overhead and faster transmission compared to TCP, making it suitable for applications where speed is prioritized over absolute reliability, such as streaming media or online gaming.

UDP Header Fields


+-----------------+-----------------+
| Source Port     | Destination Port|
+-----------------+-----------------+
| Length          | Checksum        |
+-----------------+-----------------+
            

Internet Protocol (IP)

IP is responsible for addressing and routing packets of data across networks. It defines IP addresses, which uniquely identify devices on a network, and handles the routing of packets from source to destination.

IPv4 Header Fields (Simplified)


+-----------------+-----------------+-----------------+-----------------+
| Version & IHL   | Type of Service | Total Length                      |
+-----------------+-----------------+-----------------+-----------------+
| Identification  | Flags   | Fragment Offset                 |
+-----------------+-----------------+-----------------+-----------------+
| Time to Live    | Protocol        | Header Checksum                 |
+-----------------+-----------------+-----------------+-----------------+
| Source IP Address                                   |
+-----------------+-----------------+-----------------+-----------------+
| Destination IP Address                                |
+-----------------+-----------------+-----------------+-----------------+
| Options (if any)| Padding                                             |
+-----------------+-----------------+-----------------+-----------------+
            

Sockets

A socket is an endpoint for communication. It's an abstraction that allows applications to send and receive data across a network. In TCP/IP, a socket is typically identified by a combination of an IP address and a port number.

The socket API (e.g., Berkeley sockets) provides functions for creating, binding, listening, connecting, sending, and receiving data.

Example (Conceptual C/Java-like):


// Create a TCP socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);

// Bind the socket to an address and port
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));

// Listen for incoming connections
listen(sockfd, 5);

// Accept a connection
int client_sockfd = accept(sockfd, (struct sockaddr *)NULL, NULL);

// Send and receive data...

// Close sockets
close(client_sockfd);
close(sockfd);
            

Key Protocols in the TCP/IP Suite

This diagram illustrates the layered nature of TCP/IP and some common protocols at each layer.

TCP/IP Model Diagram

Image source: Wikimedia Commons

Further Reading