Winsock Core Error Handling
Effective error handling is crucial for robust network applications developed using the Windows Sockets API (Winsock). This section covers the mechanisms Winsock provides for detecting, reporting, and diagnosing errors that can occur during socket operations.
Understanding Winsock Error Codes
Winsock errors are typically reported using specific error codes. The most common way to retrieve the last error code for a socket operation is by calling the WSAGetLastError function.
int errorCode = WSAGetLastError();
This function returns a SOCK_ERR value, which is a system-dependent error code. These codes are defined in winsock2.h and can be mapped to human-readable strings using the FormatMessage API with the FORMAT_MESSAGE_FROM_SYSTEM flag.
Common Winsock Error Codes
Here are some frequently encountered Winsock error codes:
| Error Code | Symbolic Name | Description |
|---|---|---|
10001 |
WSAHOST_NOT_FOUND |
Authoritative answer: Host not found. |
10002 |
WSATRY_AGAIN |
Non-authoritative answer: This name is not known. |
10003 |
WSANO_RECOVERY |
Non-recoverable error: This name is not recoverable. |
10004 |
WSANO_DATA |
Requested name is valid, but no data of the requested type of record associated with that name. |
10005 |
WSAEINPROGRESS |
A blocking operation is currently in progress. |
10006 |
WSAEINTR |
A blocking operation was interrupted by a call to WSACancelBlockingCall. |
10007 |
WSAEWOULDBLOCK |
Non-blocking socket operation could not be completed immediately. |
10008 |
WSAENOTSOCK |
An operation was attempted on something that is not a socket. |
10009 |
WSAEDESTADDRREQ |
Destination address required. |
10010 |
WSAEMSGSZ |
Message size too large. |
10011 |
WSAEPFNOSUPPORT |
Protocol family not supported. |
10012 |
WSAEPROTONOSUPPORT |
Protocol not supported. |
10013 |
WSAESOCKTNOSUPPORT |
Socket type not supported. |
10014 |
WSAEOPNOTSUPP |
Operation not supported on transport endpoint. |
10015 |
WSAEPOTCL |
Protocol error. |
10016 |
WSAEADDRINUSE |
Address already in use. |
10017 |
WSAEADDRNOTAVAIL |
Cannot assign requested address. |
10018 |
WSAENETDOWN |
Network is down. |
10019 |
WSAENETUNREACH |
Network is unreachable. |
10020 |
WSAENETRESET |
Network dropped connection because of reset. |
10021 |
WSAECONNABORTED |
Software caused connection abort. |
10022 |
WSAECONNRESET |
Connection reset by peer. |
10023 |
WSAENOBUFS |
No buffer space available. |
10024 |
WSAEISCONN |
Socket is already connected. |
10025 |
WSAENOTCONN |
Socket is not connected. |
10026 |
WSAESHUTDOWN |
Cannot send after socket shutdown. |
10027 |
WSAETIMEDOUT |
Connection timed out. |
10028 |
WSAECONNREFUSED |
Connection refused. |
10029 |
WSAELOOP |
Too many levels of symbolic links. |
10030 |
WSAENAMETOOLONG |
File name too long. |
10031 |
WSAEHOSTDOWN |
Host is down. |
10032 |
WSAENOTBLK |
No block devices available. |
10033 |
WSAEPIPE |
Broken pipe. |
10034 |
WSAEFAULT |
Bad address. |
10035 |
WSAENOTEMPTY |
Directory not empty. |
10036 |
WSAEINVAL |
Invalid argument. |
10037 |
WSAESOCK |
Too many open sockets. |
10038 |
WSAENFILE |
Too many open files. |
10039 |
WSAEDQUOT |
Disk quota exceeded. |
10040 |
WSAESTALE |
Stale file handle reference. |
10041 |
WSAEDESTRUCTION |
The storage control block was destroyed. |
10042 |
WSAENOMOREFILES |
No more data available. |
10043 |
WSAECANCELLED |
Operation was cancelled. |
10044 |
WSAEPROCLIM |
Too many processes. |
10045 |
WSAENOTTY |
Inappropriate I/O control operation. |
10046 |
WSAEVERIFYFAILED |
Network element is unreachable. |
10047 |
WSAEREMOTEHOSTCLOSED |
The remote host has closed the connection. |
10048 |
WSAEREMOTERESTART |
The remote host has reset the connection. |
10049 |
WSAENOCONNECT |
Unable to establish a connection with the remote host. |
10050 |
WSAEPROCNOTRUNNING |
The specified process is not running. |
10051 |
WSAEPROCESSCID |
Invalid process identifier. |
10052 |
WSAEPROCLIMITED |
A process limit was exceeded. |
10053 |
WSAEUSERSOCK |
Too many users. |
10054 |
WSAENETMASK |
Invalid network mask. |
10055 |
WSAETIMELIMIT |
Timeout value too large. |
10056 |
WSAEPKTUNE |
Packet tuning failed. |
10057 |
WSAEPROTOCOLSUPPORT |
Protocol family or type not supported. |
10058 |
WSAEADDRESSFAMILYNOTSUPPORTED |
Address family not supported. |
10059 |
WSAEWINDOWSIZE |
Socket window size invalid. |
10060 |
WSAECONNABORTED |
Connection aborted. |
10061 |
WSAECONNREFUSED |
Connection refused. |
10062 |
WSAENETUNREACHABLE |
Network unreachable. |
10063 |
WSAETIMEOUT |
Connection timed out. |
10064 |
WSAENETRESET |
Network dropped connection because of reset. |
10065 |
WSAECONNRESET |
Connection reset by peer. |
10066 |
WSAENOTSOCK |
Socket operation on non-socket. |
10067 |
WSAEDESTADDRREQ |
Destination address required. |
10068 |
WSAEMSGSIZE |
Message too long. |
10069 |
WSAEPFNOSUPPORT |
Protocol family not supported. |
10070 |
WSAEPROTONOSUPPORT |
Protocol not supported. |
10071 |
WSAESOCKTNOSUPPORT |
Socket type not supported. |
10072 |
WSAEOPNOTSUPP |
Operation not supported on transport endpoint. |
10073 |
WSAEPOTCL |
Protocol error. |
10074 |
WSAEADDRINUSE |
Address already in use. |
10075 |
WSAEADDRNOTAVAIL |
Cannot assign requested address. |
10076 |
WSAENETDOWN |
Network is down. |
10077 |
WSAENETUNREACH |
Network is unreachable. |
10078 |
WSAENETRESET |
Network dropped connection because of reset. |
10079 |
WSAECONNABORTED |
Software caused connection abort. |
10080 |
WSAECONNRESET |
Connection reset by peer. |
10081 |
WSAENOBUFS |
No buffer space available. |
10082 |
WSAEISCONN |
Socket is already connected. |
10083 |
WSAENOTCONN |
Socket is not connected. |
10084 |
WSAESHUTDOWN |
Cannot send after socket shutdown. |
10085 |
WSAETIMEDOUT |
Connection timed out. |
10086 |
WSAECONNREFUSED |
Connection refused. |
Handling Specific Error Scenarios
WSAEINPROGRESS
This error indicates that a blocking Winsock operation is currently in progress. If you are using non-blocking sockets and encounter this error, it means the operation is ongoing and you should retry later.
WSAEWOULDBLOCK
Similar to WSAEINPROGRESS, this error signifies that a non-blocking operation cannot be completed immediately. The socket is not ready for the requested operation. You should typically wait for the socket to become ready (e.g., using select or WSAAsyncSelect) before retrying.
WSAECONNRESET
This is a common error on TCP connections, indicating that the remote end has closed the connection abruptly. Your application should handle this by closing its end of the connection and potentially attempting to re-establish it.
WSAEADDRINUSE
This error occurs when an attempt is made to bind a socket to an address and port that is already in use by another socket. This can happen if a previous instance of your application terminated abnormally without releasing the socket or if another process is already using that address/port combination.
Best Practices for Error Handling
- Always check the return value of Winsock functions. A return value of
SOCKET_ERROR(orLB_ERRfor some message loop functions) usually indicates an error. - Immediately call
WSAGetLastErrorafter a Winsock function returns an error to get the specific error code. - Log errors with descriptive messages, including the error code and its symbolic name if available.
- Implement retry logic for transient errors like
WSAEINPROGRESSandWSAEWOULDBLOCK, but be mindful of infinite loops. - Gracefully handle connection-oriented errors like
WSAECONNRESETandWSAETIMEDOUTby closing connections and informing the user or retrying. - For address-related errors like
WSAEADDRINUSE, provide informative feedback to the user or implement alternative address/port selection. - Consider using Winsock's asynchronous notification mechanisms (e.g.,
WSAAsyncSelect) to avoid blocking your application's main thread while waiting for operations to complete or for network events to occur.