WSACleanup
Synopsis
int WSACleanup(void);
Terminates use of the Winsock DLL (Ws2_32.dll). After calling WSACleanup, no Winsock functions may be called except for another call to WSAStartup.
Return Value
| Value | Meaning |
|---|---|
| 0 | Success. |
| SOCKET_ERROR | Failure. Call WSAGetLastError for extended error information. |
Remarks
- Each successful call to
WSAStartupmust be balanced by a corresponding call toWSACleanup. - If an application has multiple threads that have called
WSAStartup,WSACleanupmust be called an equal number of times before the DLL can be unloaded. - Calling
WSACleanupwhile sockets are still open will cause those sockets to be closed implicitly. - This function is thread‑safe.
Example
#include <winsock2.h>
#include <stdio.h>
int main(void) {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
printf("WSAStartup failed\\n");
return 1;
}
// ... use Winsock ...
int result = WSACleanup();
if (result == SOCKET_ERROR) {
printf("WSACleanup failed: %d\\n", WSAGetLastError());
} else {
printf("Winsock cleaned up successfully\\n");
}
return 0;
}