closesocket

The closesocket function disables and releases the socket.

Syntax

int closesocket(
  _In_  SOCKET s
);

Parameters

Parameter Description
s A descriptor identifying a socket.

Return Value

Success

If no error occurs, closesocket returns zero.

Failure

If an error occurs, closesocket returns SOCKET_ERROR. A specific error code can be retrieved by calling WSAGetLastError.

Remarks

The closesocket function disables further send and receive operations on a socket.

For a stream-based socket (such as SOCK_STREAM), if there is data buffered that has been sent but not yet acknowledged by the peer, closesocket will still attempt to send that data before closing the socket. The function returns when all buffered data has been sent or when an error occurs.

For datagram sockets (such as SOCK_DGRAM), closesocket returns immediately. Any unsent data is lost.

If the socket is part of a connection, the connection is terminated.

After calling closesocket, the socket handle s is no longer valid and cannot be used for subsequent Winsock function calls.

It is important to call closesocket for every socket created with socket or WSASocket when you are finished with it, to free up system resources.

A clean shutdown can be performed using shutdown followed by closesocket. However, closesocket itself performs a graceful shutdown if possible for stream sockets.

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header winsock2.h
Library Ws2_32.lib
DLL Ws2_32.dll