closesocket

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

Syntax

int closesocket(
    SOCKET s
);

Parameters

s

Socket descriptor for a socket created by socket.

Return Value

0 (zero) if the function succeeds, SOCKET_ERROR if the function fails

Use 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 already 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, closesocket or shutdown will block until all data is delivered or the timeout is reached. If linger is off, the system will process closing to allow to continue the process as quickly as possible.

If the linger timeout is reached with still unsent data, the connection is reset. 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 all received data has been read by the application.

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

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 RTX64 2013
Header Winsock2.h
Library RtTcpip.lib