TCP/IP Basics for Windows Networking

This document provides a foundational understanding of the Transmission Control Protocol (TCP) and Internet Protocol (IP) as they relate to Windows networking programming. Understanding these core protocols is essential for developing robust and efficient network applications.

What is TCP/IP?

TCP/IP is a suite of communication protocols used to interconnect network devices on the internet and other computer networks. It is comprised of two primary protocols:

The Role of TCP

TCP is a connection-oriented protocol, meaning it establishes a connection between two endpoints before data transfer begins. This connection ensures:

TCP Handshake (Three-Way Handshake)

Before any data is exchanged, TCP establishes a connection using a process called the three-way handshake:

  1. SYN (Synchronize): The client sends a SYN packet to the server to initiate a connection.
  2. SYN-ACK (Synchronize-Acknowledge): The server responds with a SYN-ACK packet, acknowledging the client's SYN and sending its own SYN.
  3. ACK (Acknowledge): The client sends an ACK packet back to the server, acknowledging the SYN-ACK.

Once this handshake is complete, the connection is established, and data transfer can begin.

Key Takeaway: The three-way handshake ensures that both the client and server are ready to communicate and agree on initial sequence numbers for reliable data transfer.

TCP Data Transmission

Data is broken down into segments. Each segment includes:


// Conceptual representation of a TCP segment header
typedef struct {
    unsigned short source_port;
    unsigned short dest_port;
    unsigned int sequence_number;
    unsigned int acknowledgment_number;
    unsigned short data_offset : 4;
    unsigned short reserved : 6;
    unsigned short flags; // URG, ACK, PSH, RST, SYN, FIN
    unsigned short window;
    unsigned short checksum;
    unsigned short urgent_pointer;
    // Options (variable length)
    // Data
} TcpSegmentHeader;
        

The Role of IP

IP operates at a lower layer than TCP and is responsible for the logical addressing and routing of data packets across networks. It is a connectionless protocol, meaning each packet is treated independently.

Note: While IP handles the delivery of individual packets, it does not guarantee delivery or order. That's where TCP comes in.

TCP/IP Stack in Windows

Windows implements the TCP/IP protocol suite as a stack of layers. Each layer provides services to the layer above it. When an application sends data:

  1. The application data is passed down through the layers.
  2. Each layer adds its own header information (e.g., TCP header, IP header).
  3. The data is then sent over the network interface.

When data is received:

  1. The data passes up through the layers.
  2. Each layer processes its corresponding header, removing it.
  3. Eventually, the original application data is delivered to the receiving application.

Common TCP Ports

Well-known ports are used by standard applications and services: