closesocket

closesocket closes the socket indicated by the socket descriptor and releases all the resources for the socket.

Syntax

Copy
int closesocket(
    [in]    SOCKET s
);

Parameters

[in] s

The socket descriptor created by socket.

Return Value

If the function succeeds, it returns 0 (zero). If the function fails, it returns SOCKET_ERROR. Call WSAGetLastError to retrieve a specific error code.

Remarks

The TCP/IP Stack does not release resources associated with the socket until closesocket is called.

By default, a TCP socket attempts to deliver the data it was asked to send. This behavior can be changed by the SO_LINGER socket option. By default, linger is on and linger time is 60 seconds.

If linger is on, closing the socket or shutting down will block until all data is delivered or the timeout is reached. If linger is off, the system will process closing to allow the process to continue as quickly as possible.

The connection is reset if the linger timeout is reached with still unsent data. closesocket returns SOCKET_ERROR with error code WSAECONNABORTED. Linger has no effect on a non-blocking socket unless linger is on and the linger timeout is zero.

The connection is also reset if closesocket is called before the application has read all received data.

Depending on the TCP state, when a TCP socket is closed, the resources associated with TCP connection might stay in the system for a while.

If the socket is closed as part of WSACleanup or process exit, linger is set on with the timeout 0 (connections get reset).

Requirements

Minimum supported version Header Library

wRTOS 1.0 SDK

winsock2.h

RttcpipApi.lib