Microsoft Docs

Winsock Error Handling

Understanding and handling errors is crucial for developing robust Winsock applications. Winsock functions return specific error codes to indicate the cause of a failure.

Retrieving Error Codes

The primary way to retrieve a Winsock error code is by calling the WSAGetLastError function after a Winsock function returns a failure. This function returns the most recent error code for the calling thread.

int error = WSAGetLastError();

Common Winsock Error Codes

The following table lists some of the most common Winsock error codes and their meanings:

Error Code Symbol Description
10001 WSAEINTR A blocking operation was interrupted by a call to WSACancelBlockingCall.
10002 WSAEBADFILE The file handle supplied is not valid.
10003 WSAENOTSOCK An operation was attempted on something that is not a socket.
10004 WSAEDESTADDRREQ A destination address was required but was not supplied.
10005 WSAEMSGSIZE A message is too long to be sent to the Windows Sockets service provider.
10006 WSAEPROTOTYPE An incorrectly specified protocol was used with this socket.
10007 WSAENOTSOCK A disconnected-mode connection attempt was made.
10008 WSAECONNREFUSED A connection attempt failed because the connected party did not properly respond after a period of time.
10009 WSAEHOSTUNREACH A socket operation failed because the destination host is unreachable.
10010 WSAENETDOWN A socket operation failed because the network subsystem failed.
10013 WSAEACCESS An attempt was made to access a socket in a way forbidden by its access permissions.
10014 WSAEFAULT The system detected a non-valid pointer address or a buffer length of zero.
10022 WSAEINVAL An invalid argument was supplied.
10024 WSAEMFILE Too many open sockets are available.
10035 WSAEWOULDBLOCK A non-blocking socket operation could not be completed immediately.
10036 WSAEINPROGRESS A blocking Winsock 1.1 call was made, and WSAStartup was called with the WSA_FLAG_MANUAL_LINKAGE flag.
10048 WSAEADDRINUSE A specified listening socket address is already in use.
10049 WSAEADDRNOTAVAIL The attempted address is not valid in its context.
10050 WSAENETUNREACH A socket operation tried to use an unreachable network.
10051 WSAECONNRESET A connection that was actively established has been aborted by the underlying network (e.g., an underlying network reset).
10054 WSAECONNABORTED An established connection was aborted due to software error on your host machine, or the connection has been reset by the peer.
10057 WSAENOTCONN A request to send or receive data was disallowed because the socket is not connected.
10060 WSAETIMEDOUT A connection attempt failed because the connected party did not properly respond after a period of time.
10061 WSAECONNREFUSED No connection could be made because the target machine actively refused it.
10065 WSAEHOST_NOT_FOUND No such host is known.
10101 WSASYSCALLFAILURE A system call interrupted by a system call, such as a system call that returns with EINTR.
10102 WSASERVICE_NOT_FOUND A requested service was not found.
10103 WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
10104 WSAEDESTADDRREQ A destination address was required but was not supplied.
10105 WSAEINVAL An invalid argument was supplied.
10106 WSAEINVALIDPROCTABLE The procedure load table is invalid.
10107 WSAEINVALIDPROTOCOLSUPPORT You have requested a protocol that is not supported by the Windows Sockets implementation.
10108 WSAEINVALIDEXTENSION An extension was not supported.
10109 WSAEPROCESSHANGUP A process is unable to access its required function.
10110 WSAENETDOWN A socket operation failed because the network subsystem failed.
10111 WSAEWINDOWSOVERFLOW No more descriptors are available.
10112 WSAHOST_NOT_FOUND No such host is known.
10113 WSANO_DATA The requested name is valid, but no data of the requested type of record was found.

Handling Specific Errors

When an error occurs, your application should check the return value of WSAGetLastError and take appropriate action. For example, if you encounter WSAEWOULDBLOCK on a non-blocking socket, you might retry the operation later.

if (bytesSent == SOCKET_ERROR) {
    int error = WSAGetLastError();
    if (error == WSAEWOULDBLOCK) {
        // Handle non-blocking send failure, maybe queue the data
        printf("Send would block. Retrying later...\n");
    } else {
        // Handle other send errors
        fprintf(stderr, "Send failed with error: %d\n", error);
    }
}

Best Practices