Microsoft Docs

getsockopt

Header: winsock2.h

Library: Ws2_32.lib

Synopsis

int getsockopt(
    SOCKET s,
    int level,
    int optname,
    char *optval,
    int *optlen
);

Parameters

ParameterDescription
sThe descriptor identifying the socket.
levelProtocol level at which the option resides. Typical values: SOL_SOCKET, IPPROTO_TCP, IPPROTO_IP.
optnameOption name to query. Examples: SO_RCVBUF, SO_ERROR, TCP_NODELAY.
optvalPointer to a buffer that receives the option value.
optlenOn input, size of optval. On output, number of bytes actually written.

Return Value

Returns 0 on success. On failure, returns SOCKET_ERROR and the error code can be retrieved with WSAGetLastError.

Remarks

Example

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

int main() {
    WSADATA wsaData;
    SOCKET s;
    int recvBufSize = 0;
    int optLen = sizeof(recvBufSize);

    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
        printf("WSAStartup failed\\n");
        return 1;
    }

    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET) {
        printf("socket failed: %d\\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*)&recvBufSize, &optLen) == SOCKET_ERROR) {
        printf("getsockopt failed: %d\\n", WSAGetLastError());
    } else {
        printf("Receive buffer size: %d bytes\\n", recvBufSize);
    }

    closesocket(s);
    WSACleanup();
    return 0;
}

See Also