Windows Sockets 2 Error Codes

Reference for Winsock 2 API error codes and their meanings.

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

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 and WSAEWOULDBLOCK indicate issues with the timing or blocking nature of operations.
  • Access and Permissions: Codes such as WSAEACCES and WSAENOTSOCK relate to incorrect usage or permissions of socket resources.
  • Address and Connection Issues: Errors like WSAEADDRINUSE, WSAECONNREFUSED, and WSAETIMEDOUT point to problems with network addresses, connection establishment, or active connections.
  • Network State: Codes like WSAENETDOWN and WSAENETUNREACH reflect issues with the underlying network infrastructure.
  • Name Resolution: Errors like WSAHOST_NOT_FOUND and WSATRY_AGAIN are related to Domain Name System (DNS) lookups.