WinSock API Reference

The Windows Sockets API (Winsock) is a Microsoft Windows implementation of the Berkeley sockets API. It provides an application programming interface (API) for network programming. This section details the functions, structures, and constants that constitute the WinSock API for developing network applications on Windows.

Core Concepts

WinSock enables applications to communicate over a network using the TCP/IP protocol suite. It abstracts the underlying network hardware and protocols, providing a consistent interface for developers.

  • Sockets: Endpoints for communication.
  • Protocols: TCP (reliable, connection-oriented) and UDP (unreliable, connectionless).
  • Address Families: AF_INET for IPv4, AF_INET6 for IPv6.
  • I/O Control (IOCTL): For managing socket behavior.

Key Functions

Socket Creation and Initialization

  • socket(): Creates a socket.
  • WSASocket(): Creates a socket with extended options.
  • WSAStartup(): Initializes the Winsock library.
  • WSACleanup(): Cleans up the Winsock library.

Address Information

  • bind(): Assigns a local protocol address to a socket.
  • connect(): Establishes a connection to a remote socket.
  • listen(): Puts a socket into a listening state.
  • accept(): Accepts a connection on a socket.
  • gethostbyname(), getaddrinfo(): Resolve hostnames to IP addresses.

Data Transfer

  • send(), WSASend(): Sends data on a socket.
  • recv(), WSARecv(): Receives data from a socket.
  • sendto(), WSASendTo(): Sends data to a specific address.
  • recvfrom(), WSARecvFrom(): Receives data from a specific address.

Socket Options and Control

  • setsockopt(): Sets socket options.
  • getsockopt(): Gets socket options.
  • ioctlsocket(): Performs I/O control operations on a socket.

Error Handling

  • WSAGetLastError(): Retrieves the error code for the last Winsock function failure.

Common Structures

The following structures are frequently used with WinSock functions:

  • SOCKADDR / SOCKADDR_IN / SOCKADDR_IN6: Address structure.
  • WSADATA: Used by WSAStartup() to return information about the Winsock implementation.
  • FD_SET: Used for socket I/O multiplexing with select().

Example: Simple TCP Client

This is a simplified example demonstrating a basic TCP client.

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>

// Link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

int main() {
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;
    int nRet;

    // Initialize Winsock
    nRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (nRet != 0) {
        std::cerr << "WSAStartup failed: " << nRet << std::endl;
        return 1;
    }

    // Create a socket
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        std::cerr << "socket failed: " << WSAGetLastError() << std::endl;
        WSACleanup();
        return 1;
    }

    // Set up the connection
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); // Connect to localhost
    clientService.sin_port = htons(27015); // Connect to port 27015

    // Establish the connection
    nRet = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
    if (nRet == SOCKET_ERROR) {
        std::cerr << "connect failed: " << WSAGetLastError() << std::endl;
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    std::cout << "Connected successfully!" << std::endl;

    // ... send and receive data here ...

    // Cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}