Overview

The Winsock (Windows Sockets) API provides a standardized interface for network communication on Windows operating systems. It implements the Berkeley sockets API with extensions specific to Windows.

Key features:

Functions

Socket Management

Creates a new socket.Closes an existing socket.Binds a socket to a local address.Marks a socket as passive for incoming connections.Accepts an incoming connection.Initiates a connection to a remote host.Sends data on a connected socket.Receives data from a connected socket.Initializes the Winsock library.Terminates Winsock usage.
FunctionHeaderDescription
socketwinsock2.h
closesocketwinsock2.h
bindwinsock2.h
listenwinsock2.h
acceptwinsock2.h
connectwinsock2.h
sendwinsock2.h
recvwinsock2.h
WSAStartupwinsock2.h
WSACleanupwinsock2.h

Asynchronous I/O

Structures

IPv4 address.IPv6 address.File descriptor set for select.Overlapped I/O structure.Buffer descriptor for WSASend/WSARecv.Address resolution results.
StructureHeaderPurpose
sockaddr_inws2def.h
sockaddr_in6ws2def.h
fd_setwinsock2.h
WSAOVERLAPPEDwinsock2.h
WSABUFwinsock2.h
addrinfows2def.h

Constants & Error Codes

Common socket type and protocol constants:

#define SOCK_STREAM      1
#define SOCK_DGRAM       2
#define IPPROTO_TCP      6
#define IPPROTO_UDP      17
#define INADDR_ANY       0x00000000u
#define INVALID_SOCKET   (SOCKET)(~0)
#define SOCKET_ERROR     -1

Some frequent Winsock error codes (retrieved with WSAGetLastError()):

Invalid argument.Operation would block.Address already in use.Connection refused.Connection timed out.
ErrorCodeDescription
WSAEINVAL10022
WSAEWOULDBLOCK10035
WSAEADDRINUSE10048
WSAECONNREFUSED10061
WSAETIMEDOUT10060

Code Samples

Simple TCP Echo Server (Blocking)

#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")

int main() {
    WSADATA wsa;
    WSAStartup(MAKEWORD(2,2), &wsa);

    SOCKET listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    sockaddr_in server{0};
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(8080);
    bind(listenSock, (sockaddr*)&server, sizeof(server));
    listen(listenSock, 5);

    while (true) {
        SOCKET client = accept(listenSock, nullptr, nullptr);
        char buf[512];
        int received = recv(client, buf, sizeof(buf), 0);
        if (received > 0) send(client, buf, received, 0);
        closesocket(client);
    }
    closesocket(listenSock);
    WSACleanup();
    return 0;
}

Asynchronous Connect using Overlapped I/O

#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")

void AsyncConnect(const char* host, const char* port) {
    WSADATA wsa; WSAStartup(MAKEWORD(2,2), &wsa);
    SOCKET s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);

    addrinfo hints{0}, *res;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    getaddrinfo(host, port, &hints, &res);

    WSAOVERLAPPED ov = {0};
    WSABUF buf = {0};
    DWORD flags = 0;
    ConnectEx(s, res->ai_addr, (int)res->ai_addrlen, nullptr, 0, nullptr, &ov);
    // Wait for completion...
    // GetOverlappedResult(s, &ov, &bytes, TRUE, &flags);
    freeaddrinfo(res);
    closesocket(s);
    WSACleanup();
}