Windows Sockets API (Winsock)

The Windows Sockets API (Winsock) is a networking API that provides applications with access to the TCP/IP networking protocol suite. It is a powerful and flexible interface for developing network-aware applications on Windows operating systems.

Winsock provides a standardized way for applications to communicate over networks, supporting a wide range of protocols including TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). This documentation covers the essential aspects of Winsock, from basic concepts to advanced programming techniques.

Key Concepts

Core Winsock Functions

Initialization and Cleanup

Socket Creation and Management

Data Transfer

Example: Simple TCP Client

This is a simplified example demonstrating the basic steps for creating a TCP client application.


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

#pragma comment(lib, "ws2_32.lib")

int main() {
    WSADATA wsaData;
    int iResult;

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

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;
    const char *sendbuf = "This is a test message.";
    char recvbuf[512] = "";
    int recvbuflen = 512;

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

    // Resolve the server address and port
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); // Server IP address
    clientService.sin_port = htons(8080); // Server port

    // Connect to server.
    iResult = connect(ConnectSocket, (SOCKADDR *) &clientService, sizeof(clientService));
    if (iResult == SOCKET_ERROR) {
        std::cerr << "connect failed: " << WSAGetLastError() << std::endl;
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    std::cout << "Connected to server." << std::endl;

    // Send data
    iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
    if (iResult == SOCKET_ERROR) {
        std::cerr << "send failed: " << WSAGetLastError() << std::endl;
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    std::cout << "Bytes Sent: " << iResult << std::endl;

    // Receive data
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0) {
        std::cout << "Bytes received: " << iResult << std::endl;
        std::cout << "Received data: " << std::string(recvbuf, iResult) << std::endl;
    } else if (iResult == 0) {
        std::cout << "Connection closed by server." << std::endl;
    } else {
        std::cerr << "recv failed: " << WSAGetLastError() << std::endl;
    }

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

    return 0;
}
            

Advanced Topics