WSAGetLastError

Retrieves the last extended error for the calling thread.

Syntax

int WSAGetLastError(void);

Return Value

The function returns the last extended error code for the calling thread. A return value of zero indicates success. If the function is called within a context where Winsock is not initialized or if the last error was not a Winsock error, the return value is undefined.

Remarks

The WSAGetLastError function retrieves the error code for the most recent Winsock function call that failed. It is essential to call WSAGetLastError immediately after a Winsock function returns a failure indicator (e.g., SOCKET_ERROR). This is because subsequent Winsock calls can overwrite the last error code.

The error codes returned by WSAGetLastError are Winsock-specific error codes. These codes are defined in the winsock2.h header file.

Important Considerations:

  • Always call WSAGetLastError immediately after a function returns SOCKET_ERROR.
  • The error code is specific to the calling thread.
  • The Winsock DLL must be initialized by a successful call to WSAStartup before using this function.

Common Winsock Error Codes

The following table lists some common Winsock error codes that can be returned by WSAGetLastError:

WSAEINTR (10004)

A blocking operation was interrupted by a call to WSACancelAsync or a similar blocking operation.

WSAECONNRESET (10054)

An established connection was aborted by the software in your host computer.

WSAETIMEDOUT (10060)

A connection attempt failed because the connected party did not respond after a period of time. Also, if a connection is made, the failure to transmit a keep-alive packet on a connection.

WSAECONNREFUSED (10061)

A connection attempt failed because the connected party actively refused it.

WSAEWOULDBLOCK (10035)

A non-blocking socket operation could not be completed immediately.

WSAENOTCONN (10057)

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a send function which requires connection) no address was previously specified for the socket.

WSAEHOST_NOT_FOUND (11001)

Authoritative answer: Host not found.

For a comprehensive list of Winsock error codes, please refer to the official Microsoft documentation.

Requirements

Minimum supported client: Windows XP [desktop apps | UWP apps ]

Minimum supported server: Windows Server 2003 [desktop apps | UWP apps ]

Header: winsock2.h (include winsock.h)

Library: Ws2_32.lib

DLLs: Ws2_32.dll