Windows API Reference

Sockets API

The Windows Sockets API (Winsock) provides a network programming interface that supports a wide range of networking protocols, including TCP/IP. This section details the functions, structures, and constants used for socket-based network communication on Windows.

Core Concepts

Key Functions

Function Description
socket() Creates a socket that is an endpoint for communication.
bind() Associates a local address with a socket.
listen() Places a socket in a state where it can accept incoming connection requests.
accept() Accepts a connection on a socket.
connect() Establishes a connection to a remote socket.
send() / sendto() Sends data over a socket.
recv() / recvfrom() Receives data from a socket.
closesocket() Closes an existing socket.
getaddrinfo() Retrieves address information for a specified host and service.
freeaddrinfo() Frees address information previously allocated by getaddrinfo().

Important Structures

Structure Description
SOCKADDR A generic socket address structure.
SOCKADDR_IN An IPv4 socket address structure.
SOCKADDR_IN6 An IPv6 socket address structure.
WSADATA Contains information about the Winsock implementation.
ADDRINFO Structure used by getaddrinfo() to store address information.

Common Constants

Getting Started

A typical Winsock application follows these steps:

  1. Call WSAStartup() to initialize the Winsock DLL.
  2. Create a socket using socket().
  3. (For server) Bind the socket to a local address and port using bind(), and then listen for connections using listen().
  4. (For client) Connect to a remote server using connect().
  5. Send and receive data using send()/sendto() and recv()/recvfrom().
  6. Close the socket using closesocket().
  7. Call WSACleanup() to release Winsock resources.