TCP/IP Programming in Windows

This section provides comprehensive guidance and API references for developing network applications using the Transmission Control Protocol/Internet Protocol (TCP/IP) suite on Windows.

Introduction to TCP/IP

TCP/IP is the foundational protocol suite for the internet and most modern networks. It provides a reliable, connection-oriented communication service, making it suitable for a wide range of applications, from web browsing to file transfers.

Key Concepts

Core Winsock APIs

The Winsock API is your gateway to TCP/IP programming on Windows. Here are some of the most fundamental functions:

socket()

Creates a socket that is used to communicate with other applications.

SOCKET socket(
  [in] int af,
  [in] int type,
  [in] int protocol
);

Learn more...

bind()

Associates a local address with a socket.

int bind(
  [in] SOCKET s,
  [in] const struct sockaddr *name,
  [in] int namelen
);

Learn more...

connect()

Establishes a connection to a specific peer using a socket.

int connect(
  [in] SOCKET s,
  [in] const struct sockaddr *name,
  [in] int namelen
);

Learn more...

send() and recv()

Send and receive data over a connected socket.

int send(
  [in] SOCKET s,
  [in] const char *buf,
  [in] int len,
  [in] int flags
);

int recv(
  [in] SOCKET s,
  [in] char *buf,
  [in] int len,
  [in] int flags
);

Learn more... | Learn more...

listen() and accept()

For server applications, these functions listen for incoming connections and accept them.

int listen(
  [in] SOCKET s,
  [in] int backlog
);

SOCKET accept(
  [in] SOCKET s,
  [out] struct sockaddr *addr,
  [in, out] int *addrlen
);

Learn more... | Learn more...

Building a TCP Client/Server Application

Follow these general steps to create a basic TCP client and server:

  1. Client: Create a socket, resolve the server address, connect to the server, send/receive data, and close the socket.
  2. Server: Create a socket, bind it to a local address and port, listen for incoming connections, accept a connection, send/receive data, and close the client socket. Repeat accept for multiple clients.

Example: Simple Echo Server

Refer to the Echo Server Sample for a practical implementation.

Advanced Topics