Win32 Networking API Samples

TCP Client (Blocking)


// Simple TCP client using Winsock
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")

int main() {
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
                    hints;
    const char *sendbuf = "Hello from client";
    char recvbuf[512];
    int iResult, recvbuflen = 512;

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

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo("127.0.0.1", "27015", &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(result->ai_family, result->ai_socktype,
        result->ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("socket failed: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Connect to server.
    iResult = connect( ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
    }

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }

    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive data until the server closes the connection
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0)
        printf("Bytes received: %d\\n", iResult);
    else if (iResult == 0)
        printf("Connection closed\\n");
    else
        printf("recv failed: %d\\n", WSAGetLastError());

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();
    return 0;
}

TCP Server (Asynchronous, Overlapped I/O)


// Asynchronous TCP server using Winsock overlapped I/O
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

#define DEFAULT_PORT "27016"
#define BUFFER_SIZE 512

void CALLBACK WorkerRoutine(DWORD dwError, DWORD dwBytesTransferred,
    LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags) {
    // This is invoked when an async operation completes
    // For brevity, just close the connection
    SOCKET client = *(SOCKET*)lpOverlapped->hEvent;
    closesocket(client);
    printf("Client disconnected (%d bytes transferred)\\n", dwBytesTransferred);
    delete (SOCKET*)lpOverlapped->hEvent;
    delete lpOverlapped;
}

int main() {
    WSADATA wsaData;
    SOCKET ListenSocket = INVALID_SOCKET, ClientSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL, hints;
    int iResult;

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

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE; // use local address

    // Resolve the server address and port
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\\n", iResult);
        WSACleanup();
        return 1;
    }

    // Create a socket for listening
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET) {
        printf("socket failed: %ld\\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Bind the socket
    iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("bind failed: %d\\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    // Listen for connections
    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed: %d\\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    printf("Server listening on port %s\\n", DEFAULT_PORT);

    while (TRUE) {
        // Accept a client socket
        ClientSocket = accept(ListenSocket, NULL, NULL);
        if (ClientSocket == INVALID_SOCKET) {
            printf("accept failed: %d\\n", WSAGetLastError());
            break;
        }

        // Allocate overlapped structure and event handle
        WSABUF wsaBuf;
        char *recvbuf = new char[BUFFER_SIZE];
        wsaBuf.buf = recvbuf;
        wsaBuf.len = BUFFER_SIZE;

        WSAOVERLAPPED *overlapped = new WSAOVERLAPPED{0};
        overlapped->hEvent = (HANDLE)new SOCKET(ClientSocket);

        // Issue asynchronous receive
        DWORD flags = 0, recvBytes = 0;
        iResult = WSARecv(ClientSocket, &wsaBuf, 1, &recvBytes, &flags,
                          overlapped, WorkerRoutine);
        if (iResult == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
            printf("WSARecv failed: %d\\n", WSAGetLastError());
            delete[] recvbuf;
            delete overlapped;
            closesocket(ClientSocket);
        } else {
            printf("Client connected, async receive posted\\n");
        }
    }

    // Cleanup
    closesocket(ListenSocket);
    WSACleanup();
    return 0;
}

UDP Echo Server


// UDP echo server example
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")

#define SERVER_PORT "27017"
#define BUF_SIZE 512

int main() {
    WSADATA wsaData;
    SOCKET sock = INVALID_SOCKET;
    struct addrinfo *result = NULL, hints;
    char recvbuf[BUF_SIZE];
    struct sockaddr_storage clientAddr;
    int clientAddrLen = sizeof(clientAddr);
    int iResult;

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

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET; // IPv4
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_protocol = IPPROTO_UDP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve address
    iResult = getaddrinfo(NULL, SERVER_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\\n", iResult);
        WSACleanup();
        return 1;
    }

    // Create socket
    sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (sock == INVALID_SOCKET) {
        printf("socket failed: %ld\\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Bind socket
    iResult = bind(sock, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("bind failed: %d\\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);
    printf("UDP echo server listening on port %s\\n", SERVER_PORT);

    while (TRUE) {
        iResult = recvfrom(sock, recvbuf, BUF_SIZE, 0,
                           (SOCKADDR *)&clientAddr, &clientAddrLen);
        if (iResult == SOCKET_ERROR) {
            printf("recvfrom failed: %d\\n", WSAGetLastError());
            break;
        }

        // Echo back the same data
        sendto(sock, recvbuf, iResult, 0,
               (SOCKADDR *)&clientAddr, clientAddrLen);
    }

    // Cleanup
    closesocket(sock);
    WSACleanup();
    return 0;
}