WSAGetLastError Function
Retrieves the last error code set by a Winsock function.
Syntax
int WSAGetLastError(
void
);
Parameters
This function does not take any parameters.
Return Value
The return value is the last error code that was set for the calling thread. It can be one of the Winsock error codes.
Remarks
Every Winsock function that can fail sets a specific error code if an error occurs. The WSAGetLastError function retrieves this error code.
It is important to call WSAGetLastError immediately after a Winsock function returns an error, as subsequent Winsock calls can overwrite the error code.
The error codes returned by WSAGetLastError are defined in Winsock2.h and are typically prefixed with WSAE (e.g., WSAEWOULDBLOCK).
WSAGetLastError is thread-specific, meaning it retrieves the error code for the calling thread only.
Example
The following code snippet demonstrates how to use WSAGetLastError after a potential error:
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
int errorCode = WSAGetLastError();
// Handle the error, for example, by logging it
fprintf(stderr, "Socket creation failed with error code: %d\n", errorCode);
// Further error handling...
return 1;
}
Requirements
| Tool | Version |
|---|---|
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | Winsock2.h (include Winsock2.h) |
| Library | Ws2_32.lib |
| DLL | Ws2_32.dll |