MSDN Documentation

Winsock API Reference

This section provides comprehensive documentation for the Windows Sockets API (Winsock), which enables applications to communicate over a network using the TCP/IP protocol suite and other network protocols.

Introduction to Winsock

Winsock is a C-language API that provides a standardized interface for network programming on Windows. It allows developers to create client and server applications that can communicate with each other across local networks or the internet.

Key Concepts

Core Functions

The Winsock API consists of a set of functions that cover various networking operations. Here are some of the most commonly used functions:

Initialization and Cleanup

Socket Management

Data Transfer

Error Handling

Winsock functions return specific error codes that can be interpreted using WSAGetLastError.

int iResult = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (iResult == INVALID_SOCKET) {
    int errorCode = WSAGetLastError();
    // Handle error, e.g., outputting errorCode
    printf("Socket creation failed with error: %d\n", errorCode);
}
            

Advanced Topics

Note: Ensure that the Winsock DLL is properly initialized with WSAStartup before calling any other Winsock functions and that WSACleanup is called when done.

Tip: For UDP sockets, use sendto and recvfrom for datagram communication.