Introduction to Winsock

Winsock (Windows Sockets API) provides a C-style API for network programming on Windows. It enables applications to communicate over a network using standard Internet protocols such as TCP/IP and UDP. Winsock is an abstraction layer that hides the complexities of the underlying network hardware and protocols, offering a consistent interface for developers.

Core Concepts

Winsock revolves around several key concepts:

Key Functions

The Winsock API consists of numerous functions, but some of the most frequently used include:

Example: A Simple TCP Client

Here's a simplified example of how to initiate a TCP connection and send a message:


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

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

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

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // Setup the client structure
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); // Connect to localhost
    clientService.sin_port = htons(27015); // Port number of the server

    // Connect to server
    iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
    if (iResult == SOCKET_ERROR) {
        printf("connect failed with error: %s\n", gai_strerror(WSAGetLastError()));
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    printf("Successfully connected to server on 127.0.0.1:27015\n");

    // ... send/recv data here ...

    // Clean up
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}
        

Note: For robust network applications, error handling is crucial. Always check the return values of Winsock functions and use WSAGetLastError() to retrieve specific error codes.

Further Reading