Windows Sockets 2 Error Codes
This section provides a comprehensive list of error codes that can be returned by Windows Sockets 2 API functions. Understanding these codes is crucial for diagnosing and resolving network-related issues in Windows applications.
Common Error Codes
-
WSAEINTR (10004)
A blocking operation was interrupted by a call to
WSACancelAsyncRequest
. - WSAEBADF (10009) A file descriptor or handle is invalid.
- WSAEACCES (10013) An attempt was made to access a socket in a way forbidden by its access permissions.
- WSAEFAULT (10014) Bad address. The system detected an invalid pointer address in attempting to use a pointer to a block of memory.
- WSAEINVAL (10022) An invalid argument was supplied.
- WSAEMFILE (10024) Too many open sockets.
- WSAEWOULDBLOCK (10035) A non-blocking socket operation could not be completed immediately.
- WSAEINPROGRESS (10036) A blocking Winsock 1.1 operation is in progress, or the service provider is still processing a callback function.
- WSAENOTSOCK (10038) An operation was attempted on something that is not a socket.
- WSAEADDRINUSE (10048) A requested address is already in use.
- WSAECONNRESET (10054) An existing connection was forcibly closed by the remote host.
- WSAETIMEDOUT (10060) A connection attempt failed because the connected party did not properly respond after a period of time.
- WSAECONNABORTED (10053) An established connection was aborted due to error or failure on the host machine.
- WSAECONNREFUSED (10061) A connection attempt failed because the connected party did not respond.
- WSAENETDOWN (10050) The network subsystem failed.
- WSAENETUNREACH (10051) A socket operation attempted to use an unreachable network.
- WSAHOST_NOT_FOUND (11001) A DNS query failed to return a valid answer. Host not found.
- WSATRY_AGAIN (11002) A temporary failure in name resolution occurred. Try again later.
- WSANO_RECOVERY (11003) A nonrecoverable error occurred during name resolution.
Retrieving Error Codes
To retrieve the last error code for a Winsock function, use the WSAGetLastError()
function. This function should be called immediately after a Winsock function returns an error.
#include <winsock2.h>
#include <stdio.h>
// ... inside a function after a Winsock call ...
int result = socket(...);
if (result == INVALID_SOCKET) {
int errorCode = WSAGetLastError();
fprintf(stderr, "Socket creation failed with error code: %d\n", errorCode);
// You can then look up the error code in this documentation.
}
Error Code Categories
Winsock error codes can be broadly categorized:
- Operation Interruption/Completion: Errors like
WSAEINTR
andWSAEWOULDBLOCK
indicate issues with the timing or blocking nature of operations. - Access and Permissions: Codes such as
WSAEACCES
andWSAENOTSOCK
relate to incorrect usage or permissions of socket resources. - Address and Connection Issues: Errors like
WSAEADDRINUSE
,WSAECONNREFUSED
, andWSAETIMEDOUT
point to problems with network addresses, connection establishment, or active connections. - Network State: Codes like
WSAENETDOWN
andWSAENETUNREACH
reflect issues with the underlying network infrastructure. - Name Resolution: Errors like
WSAHOST_NOT_FOUND
andWSATRY_AGAIN
are related to Domain Name System (DNS) lookups.